-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1941.java
More file actions
34 lines (32 loc) · 997 Bytes
/
LeetCode1941.java
File metadata and controls
34 lines (32 loc) · 997 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
33
34
public class LeetCode1941 {
public static void main(String[] args) {
// 输入:s = "abacbc"
// 输出:true
System.out.println(new Solution1941().areOccurrencesEqual("abacbc"));
// 输入:s = "aaabb"
// 输出:false
System.out.println(new Solution1941().areOccurrencesEqual("aaabb"));
}
}
class Solution1941 {
public boolean areOccurrencesEqual(String s) {
int[] counts = new int[26];
for (int i = 0; i < s.length(); i++) {
counts[s.charAt(i) - 'a']++;
}
// System.out.println(Arrays.toString(counts));
int count = 0;
for (int i = 0; i < counts.length; i++) {
if (counts[i] != 0) {
if (count == 0) {
count = counts[i];
} else {
if (count != counts[i]) {
return false;
}
}
}
}
return true;
}
}