-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.java
More file actions
34 lines (31 loc) · 1.11 KB
/
Copy pathQuestion2.java
File metadata and controls
34 lines (31 loc) · 1.11 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
package practice2_hashing;
import java.util.Arrays;
import java.util.HashMap;
public class Question2 {
public int[] solution(String s){
int[] answer = new int[5];
HashMap<Character, Integer> sH = new HashMap<>();
for(char x : s.toCharArray()){
sH.put(x, sH.getOrDefault(x, 0)+1);
}
int max = Integer.MIN_VALUE;
String tmp = "abcde";
for(char key : tmp.toCharArray()){
if(sH.getOrDefault(key, 0) > max){
max = sH.getOrDefault(key, 0);
}
}
for(int i = 0; i < tmp.length(); i++){
answer[i] = max - sH.getOrDefault(tmp.charAt(i), 0);
}
return answer;
}
public static void main(String[] args){
Question2 T = new Question2();
System.out.println(Arrays.toString(T.solution("aaabc")));
System.out.println(Arrays.toString(T.solution("aabb")));
System.out.println(Arrays.toString(T.solution("abcde")));
System.out.println(Arrays.toString(T.solution("abcdeabc")));
System.out.println(Arrays.toString(T.solution("abbccddee")));
}
}