Weighted Uniform Strings

  • + 3 comments
    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
        // Write your code here
    		// Youtube : The Adarsh 1M
        Set<Integer>hs = new HashSet<>();
            int prev = 0;
            char cho = s.charAt(0);
            // Map<Character, Integer> mp = new HashMap<>();
            for(char ch : s.toCharArray()) {
                int val = (ch-'a') + 1;
                if(ch == cho){
                    val = val+prev;
                    prev = val;
                }
                else{
                    prev = val;
                }
                cho = ch;
                hs.add(val);
                // mp.put(ch, val);
            }
            List<String> ans = new ArrayList<>();
            for(int num : queries) {
                if(hs.contains(num))
                    ans.add("Yes");
                else
                    ans.add("No");
            }
            return ans;
    
        }