-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check if frequencies can be equal
79 lines (60 loc) · 1.62 KB
/
Check if frequencies can be equal
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
67
68
69
70
71
72
73
74
75
76
77
78
79
package gfg;
//Check if frequencies can be equal
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String input[] = read.readLine().split(" ");
String a = input[0];
Solution ob = new Solution();
if(ob.sameFreq(a)){
System.out.println(1);
}
else{
System.out.println(0);
}
}
}
}
class Solution {
boolean sameFreq(String s) {
int [] freq = new int[26];
for(char c: s.toCharArray()) {
freq[c - 'a']++;
}
if(check(freq)) {
return true;
}
else {
for(int i = 0; i < 26; i++) {
if(freq[i] > 0) {
freq[i]--;
if(check(freq)) {
return true;
}
else {
freq[i]++;
}
}
}
}
return false;
}
boolean check(int[] arr) {
int n = -1;
for(int f: arr) {
if(f > 0) {
if(n == -1) {
n = f;
}
else if(f != n) {
return false;
}
}
}
return true;
}
}