HackerRank Weighted Uniform Strings Solution Yashwant Parihar, April 25, 2023April 28, 2023 In this post, we will solve HackerRank Weighted Uniform Strings Solution. A weighted string is a string of lowercase English letters where each letter has a weight. Character weights are 1 to 26 from a to z as shown below: The weight of a string is the sum of the weights of its characters. For example: A uniform string consists of a single character repeated zero or more times. For example, ccc and a are uniform strings, but bcb and cd are not. A pangram is a string that contains every letter of the alphabet. Given a sentence determine whether it is a pangram in the English alphabet. Ignore case. Return either pangram or not pangram as appropriate.Examples = ‘The quick brown fox jumps over the lazy dog’The string contains all letters in the English alphabet, so return pangram. string weight a 1 b 2 bb 4 c 3 cc 6 ccc 9 d 4 dd 8 ddd 12 dddd 16 Now for each value in quires , see if it exists in the possible string weights. The return array is ['Yes', 'No', 'No', 'Yes', 'No']. Function Description Complete the weightedUniformStrings function in the editor below. weightedUniformStrings has the following parameter(s):– string s: a string– int queries[n]: an array of integers Returns– string[n]: an array of strings that answer the queries Input FormatThe first line contains a string s. the original string.The second line contains an integer n the number of queries.Each of the next lines contains an integer queries[i], the weight of a uniform subtring ofs that may or may not exist. Sample Input 0 abccddde 6 1 3 12 5 9 10 Sample Output 0 Yes Yes Yes Yes No No Explanation 0 The weights of every possible uniform substring in the string abccddde are shown below: We print Yes on the first four lines because the first four queries match weights of uniform substrings of s. We print No for the last two queries because there are no uniform substrings in s that have those weights.Note that while de is a substring of s that would have a weight of 9, it is not a uniform substring.Note that we are only dealing with contiguous substrings. So ccc is not a substring of thestring ccxxc. Sample Input 1 aaabbbbcccddd 5 9 7 8 12 5 Sample Output 1 Yes No Yes Yes No HackerRank Weighted Uniform Strings Problem Solution Weighted Uniform Strings C Solution // Weight Uniform String #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char s[100000]; scanf("%s", &s); long long int tam; tam = strlen(s); long long int i, j; int reps[26]; int conta = 0; int max = 0; for ( i = 'a'; i <= 'z'; ++i ) { for ( j = 0; j < tam; ++j ) { if ( s[j] == i ) { ++conta; } else { conta = 0; } if ( max < conta ) { max = conta; } } reps[i-97] = max; max = 0; conta = 0; } int n; scanf("%d", &n); int alfa[n]; for ( i = 0; i < n; ++i) { scanf("%d", &alfa[i]); } int cen = 0; for ( i = 0; i < n; ++i ) { for ( j = 0; j < 26; ++j ) { if ( alfa[i]%(j+1) == 0 && reps[j]*(j+1) >= alfa[i] ) { cen = 1; j = 26; } } if ( cen == 1 ) { printf("Yes\n"); } else { printf("No\n"); } cen = 0; } } Weighted Uniform Strings C++ Solution #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <set> using namespace std; int main() { string a; cin >> a; set<int> vis; int temp=1; for(int i = 1;i<a.size();i++){ vis.insert(temp*(a[i-1]-'a'+1)); if(a[i]==a[i-1]){ temp++; } else{ temp=1; } } vis.insert(temp*(a[a.size()-1]-'a'+1)); int n; cin >> n; for(int i = 0;i<n;i++){ int x; cin >> x; if(vis.find(x)!=vis.end()){ cout << "Yes\n"; }else{ cout << "No\n"; } } return 0; } Weighted Uniform Strings C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static Dictionary<char, byte> chars = new Dictionary<char, byte>() { ['a'] = 1, ['b'] = 2, ['c'] = 3, ['d'] = 4, ['e'] = 5, ['f'] = 6, ['g'] = 7, ['h'] = 8, ['i'] = 9, ['j'] = 10, ['k'] = 11, ['l'] = 12, ['m'] = 13, ['n'] = 14, ['o'] = 15, ['p'] = 16, ['q'] = 17, ['r'] = 18, ['s'] = 19, ['t'] = 20, ['u'] = 21, ['v'] = 22, ['w'] = 23, ['x'] = 24, ['y'] = 25, ['z'] = 26, }; static void Main(String[] args) { string s = Console.ReadLine(); SortedSet<int> set = new SortedSet<int>(); int cnt = 1; for (int i = 1; i < s.Length; i++) { if (s[i] == s[i - 1]) { cnt++; } else { while (cnt > 0) { set.Add(cnt-- * chars[s[i - 1]]); } cnt = 1; } } while (cnt > 0) { set.Add(cnt-- * chars[s[s.Length - 1]]); } int n = Convert.ToInt32(Console.ReadLine()); for (int a0 = 0; a0 < n; a0++) { int x = Convert.ToInt32(Console.ReadLine()); if (set.Contains(x)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } } Weighted Uniform Strings Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'weightedUniformStrings' function below. * * The function is expected to return a STRING_ARRAY. * The function accepts following parameters: * 1. STRING s * 2. INTEGER_ARRAY queries */ } public class Solution { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); String s = in.next(); int n = in.nextInt(); Set<Integer> sumList = new HashSet<>(); char prev = s.charAt(0); sumList.add(prev - 'a' + 1); int sum = prev - 'a' + 1; for (int i = 1; i < s.length(); i++) { char next = s.charAt(i); if (next == prev) { sum += (prev - 'a' +1); sumList.add(sum); } else { sumList.add(next - 'a' + 1); prev = next; sum = prev - 'a' + 1; } } for (int a0 = 0; a0 < n; a0++) { int x = in.nextInt(); if (sumList.contains(x)) System.out.println("Yes"); else System.out.println("No"); } } } Weighted Uniform Strings JavaScript Solution process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function calcUniW(s) { var prChar=' '; var w = 0; var wArr = []; //var word = ''; for (let i=0; i < s.length; i++) { if (prChar !== s[i]) { word = ''; w = s[i].charCodeAt() - 'a'.charCodeAt()+1; //console.log(s[i]+': '+w); wArr.push(w); prChar = s[i]; //word = s[i]; } else { w += s[i].charCodeAt() - 'a'.charCodeAt()+1; //word += s[i]; //console.log(word+': '+w); wArr.push(w); prChar = s[i]; } } return wArr; } function main() { var s = readLine(); var wArr = calcUniW(s); var n = parseInt(readLine()); for(var a0 = 0; a0 < n; a0++){ var x = parseInt(readLine()); // your code goes here if (wArr.indexOf(x) !== -1) { console.log("Yes"); } else { console.log("No"); } } } Weighted Uniform Strings Python Solution #!/bin/python3 import sys s = input().strip() n = int(input().strip()) weight = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26] hashset = set() val = weight[ord(s[0]) - ord("a")] hashset.add(val) length_of_s = len(s) for i in range(1,length_of_s): if s[i] == s[i-1]: hashset.add(val) val += weight[ord(s[i]) - ord("a")] else: hashset.add(val) val = weight[ord(s[i]) - ord("a")] if i == length_of_s-1: hashset.add(val) for a0 in range(n): x = int(input().strip()) # your code goes here if x in hashset: print("Yes") else: print("No") Other Solutions HackerRank Separate the Numbers Solution HackerRank Funny String Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython