Weighted Uniform Strings

  • + 0 comments

    **python ** def weightedUniformStrings(s, queries): """ Determines if each query value is a valid weight of a substring in the given string.

    Args:
        s (str): The input string.
        queries (list): A list of query values.
    
    Returns:
        list: A list of "Yes" or "No" for each query, indicating whether the query value is a valid weight.
    """
    
    weights = set()
    weight = ord(s[0]) - 96
    weights.add(weight)
    
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            weight += ord(s[i]) - 96
        else:
            weight = ord(s[i]) - 96
        weights.add(weight)
    
    return ["Yes" if q in weights else "No" for q in queries]
    

    if name == 'main': s = input() queries_count = int(input().strip())

    queries = []
    
    for _ in range(queries_count):
        queries_item = int(input().strip())
        queries.append(queries_item)
    
    result = weightedUniformStrings(s, queries)
    
    print('\n'.join(result))