-
Notifications
You must be signed in to change notification settings - Fork 2
/
CheckEqualFrequencies.java
45 lines (41 loc) · 1.04 KB
/
CheckEqualFrequencies.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
//User function Template for Java
class Solution {
boolean sameFreq(String s) {
// code here
int[] freq = new int[26];
// Count freq.. of all distinct chars
for(char k : s.toCharArray()){
freq[k - 'a'] += 1;
}
// check for same freq..
if(check(freq)){
return true;
}
// reduce freq. of all char 1 by 1
for(int i = 0; i < 26; i++){
if(freq[i] > 0){
freq[i] -= 1;
if(check(freq)){
return true;
}
freq[i] += 1;
}
}
return false;
}
boolean check(int freq[]){
// first char freq. ==> set to -1 initially
int N = -1;
for(int i : freq){
if(i > 0){
if(N == -1){
N = i;
}
else if(i != N){
return false;
}
}
}
return true;
}
}