forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
57 lines (43 loc) · 1.33 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Problem: https://www.hackerrank.com/challenges/weighted-uniform-string
//Java 8
/*
Initial Thoughts:
Build a HashSet of the weights by iterating through
the string keeping track of weight O(s) time and space
Then iterate through our queries checking if they
are in the HashSet O(n) time and O(1) space
Time Complexity: O(s + n)
Space Complexity: O(n)
*/
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();
//Build a set of weights
Set<Integer> weights = new HashSet<>();
int currentWeight = 0;
char prevLetter = ' ';
for(char letter : s.toCharArray())
{
if(letter != prevLetter)
currentWeight = letter - 'a' + 1;
else
currentWeight += letter - 'a' + 1;
prevLetter = letter;
weights.add(currentWeight);
}
for(int a0 = 0; a0 < n; a0++){
int x = in.nextInt();
if(weights.contains(x))
System.out.println("Yes");
else
System.out.println("No");
}
}
}