-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.java
88 lines (72 loc) · 2.59 KB
/
solution.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Implemetation Part
static String isValid(String s) {
// using hashmap which contains -> (char, frequency)
// if key is not present then put it with freqency = 1
// else increase it's frequency by 1
Map<Character, Integer> frequencies = new HashMap<>();
for(char letter : s.toCharArray())
{
if(frequencies.containsKey(letter))
frequencies.put(letter, frequencies.get(letter) + 1);
else
frequencies.put(letter, 1);
}
// set not consider duplicate values
// so inserting all frequencies to a set valid
Set<Integer> st = new HashSet<>();
for(int freq : frequencies.values())
{
st.add(freq);
}
// if size of set > 2 then string is not
// if size = 1 then valid
if(st.size() > 2)
return ("NO");
else if(st.size() == 1)
return ("YES");
else
{
// different cases for size = 2
int f1 = 0, f2 = 0, f1Count = 0, f2Count = 0, i = 0;
for(int n : st)
{
if(i == 0) f1 = n;
else f2 = n;
i++;
}
// counting all number of frequencies, f1 nad f2
for(int freq : frequencies.values())
{
if(freq == f1) f1Count++;
if(freq == f2) f2Count++;
}
// if frequency count and fequency both = 1, then valid
// if count = 1 and difference is also = 1 then it is valid
// for another cases it is not valid
if((f1 == 1 && f1Count == 1 ) || (f2 == 1 && f2Count == 1 ))
return ("YES");
else if ((Math.abs(f1 - f2) == 1) && (f1Count == 1 || f2Count == 1))
return ("YES");
else
return ("NO");
}
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = scanner.nextLine();
String result = isValid(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}