Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
I will find a list of all substrings, which contains same following characters. Then I will create all weights – When a substring has lengths of n
, I will create n
parts. This will give me all weights of a substring.
At the end I will find if the weights contains any of the given queries.
Example
"abccddde"
:
– all substrings
— “a” (weight of 1 unit for a character)
— “b” (weight of 2 units for a character)
— “cc” (weight of 3 units for a character)
— “ddd” (weight of 4 units for a character)
— “e” (weight of 5 units for a character)
All weights for all substring parts
– “a” -> 1
– “b” -> 2
– “cc” -> 1 * 3, 2 * 3 -> 3, 6
– “ddd” -> 1 *4, 2 * 4, 3 * 4 -> 4, 8, 12
– “e” -> 5
All weights : 1, 2, 3, 4, 5, 6, 8, 12
I created solution in:
All solutions are also available on my GitHub profile.
Scala
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 27 |
import scala.io.Source object Solution extends App { val lines = Source.stdin.getLines().toList val string = lines.head val queries = lines.tail.tail.map(_.toInt) val subs = uniformSubstrings(string) val substringWeights = subs.flatMap(s => { val c = s.charAt(0).toInt - 'a'.toInt + 1 (1 to s.length).map(i => i * c) }).toSet queries.foreach(q => { val msg = if(substringWeights.contains(q)) "Yes" else "No" println(msg) }) def uniformSubstrings(s: String): List[String] = { if(s.length <= 0) { return Nil } val c = s.charAt(0) val substring = s.takeWhile(_ == c) substring :: uniformSubstrings(s.drop(substring.length)) } } |
Java
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; public class WeightedUniformStrings { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String line = stdin.nextLine(); List<Integer> queries = new ArrayList<>(); while (stdin.hasNext()) { int q = Integer.parseInt(stdin.nextLine()); queries.add(q); } queries = queries.subList(1, queries.size()); List<String> subs = uniformSubstrings(line, new ArrayList<>()); Set<Integer> substringWeights = subs.stream().flatMap(s -> { int c = s.charAt(0) - 'a' + 1; List<Integer> lengts = new ArrayList<>(); for(int i = 1; i <= s.length(); i++) { lengts.add(i * c); } return lengts.stream(); }).collect(Collectors.toSet()); for(int q : queries) { if(substringWeights.contains(q)) { System.out.println("Yes"); } else { System.out.println("No"); } } } private static List<String> uniformSubstrings(String s, List<String> strings) { if(s.length() > 0) { char c = s.charAt(0); int substringIndex = takeWhile(s, c); String substring = s.substring(0, substringIndex); strings.add(substring); return uniformSubstrings(s.substring(substringIndex), strings); } return strings; } private static int takeWhile(String s, char c) { for(int i = 0; i < s.length(); i++) { if(s.charAt(i) != c) { return i; } } return s.length(); } } |
Javascript
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
'use strict'; Array.prototype.flatMap = function(lambda) { return Array.prototype.concat.apply([], this.map(lambda)); }; const processData = input => { const lines = input.split('\n'); const line = lines[0]; const queries = lines.slice(2).map(s => parseInt(s)); const subs = uniformSubstrings(line, []); const substringWeights = subs.flatMap(s => { const c = s.charCodeAt(0) - 'a'.charCodeAt(0) + 1; const lengts = []; for(let i = 1; i <= s.length; i++) { lengts.push(i * c); } return lengts; }); for(let q = 0; q < queries.length; q++) { const query = queries[q]; if(substringWeights.indexOf(query) != -1) { console.log("Yes"); } else { console.log("No"); } } }; const uniformSubstrings = (s, strings) => { if(s.length > 0) { const c = s.charAt(0); const substringIndex = takeWhile(s, c); const substring = s.slice(0, substringIndex); const substring2 = s.substring(substringIndex); strings.push(substring); return uniformSubstrings(substring2, strings); } return strings; } const takeWhile = (s, c) => { for(let i = 0; i < s.length; i++) { if(s.charAt(i) !== c) { return i; } } return s.length; } process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |