-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2423.java
66 lines (61 loc) · 1.96 KB
/
_2423.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
58
59
60
61
62
63
64
65
66
package com.fishercoder.solutions.thirdthousand;
import java.util.Arrays;
public class _2423 {
public static class Solution1 {
/*
* This is my original, but unnecessarily verbose solution.
* Instead, you can just brute force each one of the 26 letters, as long as any one of them makes it meet the requirement, it returns true.
*/
public boolean equalFrequency(String word) {
int[] count = new int[26];
for (char c : word.toCharArray()) {
count[c - 'a']++;
}
Arrays.sort(count);
return decLast(count) || decFirst(count) || allOnes(count);
}
private boolean allOnes(int[] count) {
for (int i : count) {
if (i != 1 && i != 0) {
return false;
}
}
return true;
}
private boolean decFirst(int[] count) {
int start = 0;
int firstVal = -1;
for (int i = 0; i < 26; i++) {
if (count[i] != 0) {
start = i + 1;
firstVal = count[i] - 1;
break;
}
}
if (firstVal == 0) {
int nextVal = count[start++];
for (; start < 26; start++) {
if (count[start] != nextVal) {
return false;
}
}
return true;
}
for (; start < 26; start++) {
if (count[start] != firstVal) {
return false;
}
}
return true;
}
private boolean decLast(int[] count) {
int last = count[25] - 1;
for (int i = 24; i >= 0; i--) {
if (count[i] != 0 && count[i] != last) {
return false;
}
}
return true;
}
}
}