Weighted Uniform Strings

Sort by

recency

|

27 Discussions

|

  • + 0 comments

    Python

    def weightedUniformStrings(s, queries):
        weights = []
        
        for i, char in enumerate(s):
            #compare unicode value of char to unicode of 'a' and add 1 to find weight
            weight = ord(char) - ord('a') + 1
            if i > 0 and s[i-1] == char:
                weight += weights[-1]
            weights.append(weight)
        
        #get a set of the unique weights
        weights = set(weights)
        
        return ['Yes' if query in weights else 'No' for query in queries]
    
  • + 0 comments

    TypeScript

    function weightedUniformStrings(s: string, queries: number[]): string[] {
      let weightOjb = {} as any;
      let windowStart = 0;
    
      while (windowStart < s.length) {
        const charWeight = s[windowStart].charCodeAt(0) - 96;
        // add a new element
        weightOjb[charWeight] = 1;
    
        // record uniform string number
        let windowEnd = windowStart + 1;
        let weight = charWeight;
        while (s[windowStart] === s[windowEnd]) {
          weight += charWeight;
          weightOjb[weight] = 1;
          windowEnd++;
        }
        windowStart = windowEnd;
      }
    
      const result = queries.map((query) =>
        weightOjb.hasOwnProperty(query) ? 'Yes' : 'No'
      );
    
      return result;
    }
    
  • + 1 comment

    Pythonic:

    def weightedUniformStrings(s, queries):
        mem = accumulate(range(1, len(s)), lambda c, i: c+1 if s[i] == s[i-1] else 1, initial=1)
        mem = set(m[1] *(ord(m[0]) - 96) for m in zip(s, mem))
        
        return ['Yes' if q in mem else 'No' for q in queries]
    
  • + 0 comments
    def weightedUniformStrings(s, queries):
        out = []
        variants = set()
        prev = None
        for value in s:
            code_point = ord(value) - 96
            if value != prev:
                temp = code_point
            else:
                temp += code_point
            prev = value
            variants.add(temp)
        [out.append('Yes' if x in variants else 'No') for x in queries]
        return out
    
  • + 0 comments

    Java O(n + m)

    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            List<String> result = new ArrayList<>();
            Set<Integer> weights = new HashSet<>();
            int count = 1;
            char prev = s.charAt(0);
            weights.add(prev - 'a' + 1);
    
            for (int i = 1; i < s.length(); i++) {
                char curr = s.charAt(i);
                if (curr == prev) {
                    count++;
                } else {
                    count = 1;
                    prev = curr;
                }
                weights.add((curr - 'a' + 1) * count);
            }
    
            for (int query : queries) {
                result.add(weights.contains(query) ? "Yes" : "No");
            }
    
            return result;
        }