-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion3.java
More file actions
32 lines (29 loc) · 983 Bytes
/
Copy pathQuestion3.java
File metadata and controls
32 lines (29 loc) · 983 Bytes
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
package practice2_hashing;
import java.util.*;
public class Question3 {
public int solution(String s){
int answer = 0;
HashMap<Character, Integer> sH = new HashMap<>();
HashSet<Integer> ch = new HashSet<>();
for(char x : s.toCharArray()){
sH.put(x, sH.getOrDefault(x, 0) + 1);
}
for(char key : sH.keySet()){
while(ch.contains(sH.get(key))){
answer++;
sH.put(key, sH.get(key) - 1);
}
if(sH.get(key) == 0) continue;
ch.add(sH.get(key));
}
return answer;
}
public static void main(String[] args){
Question3 T = new Question3();
System.out.println(T.solution("aaabbbcc"));
System.out.println(T.solution("aaabbc"));
System.out.println(T.solution("aebbbbc"));
System.out.println(T.solution("aaabbbcccde"));
System.out.println(T.solution("aaabbbcccdddeeeeeff"));
}
}