-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckThreeAndTwo.java
More file actions
31 lines (27 loc) · 999 Bytes
/
checkThreeAndTwo.java
File metadata and controls
31 lines (27 loc) · 999 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
// Check three and two
// DESCRIPTION:
// Given an array with exactly 5 strings "a", "b" or "c" (chars in Java, characters in Fortran), check if the array contains three and two of the same values.
// Examples
// ["a", "a", "a", "b", "b"] ==> true // 3x "a" and 2x "b"
// ["a", "b", "c", "b", "c"] ==> false // 1x "a", 2x "b" and 2x "c"
// ["a", "a", "a", "a", "a"] ==> false // 5x "a"
import java.util.HashMap;
import java.util.*;
public class Solution {
public boolean checkThreeAndTwo(char[] chars) {
// your code
HashMap<Character, Integer> letterCounts = new HashMap<Character, Integer>();
for(int i = 0; i < chars.length; i++){
if(!letterCounts.containsKey(chars[i])){
letterCounts.put(chars[i], 1);
}else{
letterCounts.put(chars[i], letterCounts.get(chars[i]) + 1);
}
}
if(letterCounts.containsValue(2) && letterCounts.containsValue(3)){
return true;
}else{
return false;
}
}
}