-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWeightedUniformStrings.java
More file actions
48 lines (38 loc) ยท 1.19 KB
/
Copy pathWeightedUniformStrings.java
File metadata and controls
48 lines (38 loc) ยท 1.19 KB
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
package hackerrank;
import java.util.HashSet;
import java.util.Set;
public class WeightedUniformStrings {
static String[] weightedUniformStrings(String s, int[] queries) {
int[] charsWeight = new int[26];
for(int i=0; i<26; i++){
charsWeight[i] = i + 1;
}
int currentChar = s.charAt(0);
int currentWeight = charsWeight[currentChar - 'a'];
Set<Integer> allWeights = new HashSet<Integer>();
allWeights.add(currentWeight);
for(int i=1; i<s.length(); i++){
currentChar = s.charAt(i);
char prevChar = s.charAt(i-1);
allWeights.add(charsWeight[currentChar -'a']);
if(prevChar == currentChar){
currentWeight += charsWeight[currentChar - 'a'];
} else {
currentWeight = charsWeight[currentChar - 'a'];
}
allWeights.add(currentWeight);
}
String[] result = new String[queries.length];
for(int i=0; i<queries.length; i++){
if(allWeights.contains(queries[i])){
result[i] = "Yes";
}else {
result[i] = "No";
}
}
return result;
}
public static void main(String[] args) {
System.out.println(weightedUniformStrings("abccddde", new int[]{6, 1, 3, 12, 5, 9, 10}));
}
}