Weighted Uniform Strings

Sort by

recency

|

26 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    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;
        }
    
  • + 0 comments
    function weightedUniformStrings(s, queries) {
        const validWeights = new Set();
        let currentChar = '';
        let currentWeight = 0;
        let count = 0;
    
        for (let i = 0; i < s.length; i++) {
            if (s[i] === currentChar) {
                count++;
            } else {
                currentChar = s[i];
                count = 1;
                currentWeight = s.charCodeAt(i) - 96; // 'a' has ASCII value 97
            }
    
            validWeights.add(currentWeight * count);
        }
    
        return queries.map(q => validWeights.has(q) ? 'Yes' : 'No');
    }