-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
39 lines (34 loc) · 1.12 KB
/
Solution.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
// github.com/RodneyShag
import java.util.Scanner;
import java.util.HashSet;
// Traverse the string just 1 time. Save the weights of every possible uniform substring in a HashSet.
// Time complexity: O(n)
// Space complexity: O(n)
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
int n = scan.nextInt();
HashSet<Integer> weights = getWeights(str);
while (n-- > 0) {
int x = scan.nextInt();
System.out.println(weights.contains(x) ? "Yes" : "No");
}
scan.close();
}
private static HashSet<Integer> getWeights(String str) {
HashSet<Integer> weights = new HashSet();
int weight = 0;
char prev = ' '; // so it doesn't match 1st character
for (int i = 0; i < str.length(); i++) {
char curr = str.charAt(i);
if (curr != prev) {
weight = 0;
}
weight += curr - 'a' + 1;
weights.add(weight);
prev = curr;
}
return weights;
}
}