In this HackerRank Weighted Uniform Strings, problem Given a string, s, let U be the set of weights for all possible uniform contiguous substrings of string s. There will be n queries to answer where each query consists of a single integer. Create a return array where for each query, the value is Yes if query[i] related to U. Otherwise, append No.
Problem solution in Python programming.
#!/bin/python3 import sys from collections import Counter def weight_of_letter(l): return ord(l) - 96 def get_uniform_subsets(s): cnt = Counter() curr = 0 last = "" for ch in s: if ch == last: curr += 1 else: last = ch curr = 1 if curr > cnt[ch]: cnt[ch] = curr weights = set() for i in cnt: for j in range(cnt[i]): weights.add((j+1)*weight_of_letter(i)) return weights s = input().strip() weights = get_uniform_subsets(s) n = int(input().strip()) for a0 in range(n): x = int(input().strip()) # your code goes here if x in weights: print("Yes") else: print("No")
Problem solution in Java Programming.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); int n = in.nextInt(); HashSet<Integer> map = new HashSet<Integer>(); int i, j, curr; i = 0; while (i < s.length()) { j = i; curr = 0; while (j < s.length() && s.charAt(j) == s.charAt(i)) { curr += s.charAt(j++) - 'a' + 1; map.add(curr); } i = j; } for(int a0 = 0; a0 < n; a0++){ int x = in.nextInt(); // your code goes here if (map.contains(x)) System.out.println("Yes"); else System.out.println("No"); } } }
Problem solution in C++ programming.
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int maxlen[26] = {}; int main(){ string s; cin >> s; int len = s.size(); int i = 0; while (i < len) { int j = i; while (j < len && s[i] == s[j]) j++; maxlen[s[i] - 'a'] = max(maxlen[s[i] - 'a'], j - i); i = j; } int n; cin >> n; for(int a0 = 0; a0 < n; a0++){ int x; cin >> x; bool poss = false; for (i = 0; i < 26; i++) { if (x % (i + 1) == 0) { int y = x / (i + 1); poss |= (y <= maxlen[i]); } } cout << (poss ? "Yes" : "No") << endl; } return 0; }
Problem solution in C programming.
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ char* s = (char *)malloc(512000 * sizeof(char)); scanf("%s",s); int n; scanf("%d",&n); int i; int *c = (int *)malloc(10000000 * sizeof(int)); int sum; i = -1; while (s[++i] != ' ') { if (i > 0 && s[i - 1] == s[i]) sum = (s[i] - 96) + sum; else sum = s[i] - 96; c[sum - 1] = sum; } free(s); for(int a0 = 0; a0 < n; a0++){ int x; scanf("%d",&x); // your code goes here if (c[x - 1] == x) printf("Yesn"); else printf("Non"); } return 0; }
Problem solution in JavaScript programming.
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 main() { var s = readLine(); var n = parseInt(readLine()); let alphabet = "abcdefghijklmnopqrstuvwxyz"; let weights = {}; alphabet.split("").forEach((letter, index) => { weights[letter] = index+1; }); let blocks = {}; let currentSum = 0; let currentLetter = s[0]; for(let i = 0;i<s.length;i++){ currentSum += weights[currentLetter]; blocks[currentSum] = true; if(i+1 < s.length && s[i+1] !== currentLetter){ currentSum = 0; currentLetter = s[i+1]; } } for(var a0 = 0; a0 < n; a0++){ var x = parseInt(readLine()); // your code goes here if(blocks[x] === true){ console.log("Yes"); }else{ console.log("No"); } } }