hackerrank weighted uniform string

This commit is contained in:
anebz 2019-12-10 17:22:44 +01:00
parent 8c1c109188
commit 6f1518cc33
1 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,50 @@
# Algorithms > Strings > Weighted Uniform Strings
# Check if the number belongs to the set of weights for all possible uniform strings in input
#
# https://www.hackerrank.com/challenges/weighted-uniform-string/problem
import math
import os
import random
import re
import sys
# Complete the weightedUniformStrings function below.
def weightedUniformStrings(s, queries):
weights = set()
weights.add(ord(s[0]) - 96)
count = 1
for i in range(1, len(s)):
weight = ord(s[i]) - 96
if s[i] != s[i-1]:
count = 1
else:
count += 1
weights.add(weight * count)
res = []
for q in queries:
res.append("Yes" if q in weights else "No")
return res
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
queries_count = int(input())
queries = []
for _ in range(queries_count):
queries_item = int(input())
queries.append(queries_item)
result = weightedUniformStrings(s, queries)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()