We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Weighted Uniform Strings
  5. Discussions

Weighted Uniform Strings

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • sphericalconic
    4 months ago+ 1 comment

    JAVA

    public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
            Set<Integer> weights = new HashSet<>();
            int count = 1;
            List<String> result = new ArrayList<>();
            for (int i = 0; i < s.length(); i++) {
                weights.add(count * ((int) s.charAt(i) - 96));
                if (i + 1 < s.length() && s.charAt(i) == s.charAt(i + 1)) {
                    count++;
                } else {
                    count = 1;
                }
            }
            for (Integer query : queries) {
                if (weights.contains(query)) {
                    result.add("Yes");
                } else {
                    result.add("No");
                }
            }
            return result;
        }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy