-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.java
97 lines (78 loc) · 2.38 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
88
89
90
91
92
93
94
95
96
97
//Problem: https://www.hackerrank.com/challenges/sherlock-and-valid-string
//Java 8
/*
Initial Thoughts:
Get every chars' frequency
If there are more than two different frequencies
NO
if 1 frequency
YES
if 2 frequency
if 1 occurs only once and frequency is 1
yes
else
if their difference 1 and one has frequency 1
yes
else
no
examples:
abcde -> Y
a -> Y
aabb -> Y
aaaabbbbc -> Y
aaaabbbbcd -> N
aabbcd -> N
Time Complexity: O(n) //We have to look at every char
Space Complexity: O(n) //We store frequencies in a Hashmap
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
String s = input.nextLine();
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<Integer> st = new HashSet<>();
for(int freq : frequencies.values())
{
st.add(freq);
}
if(st.size() > 2)//More than 2 frequencies
System.out.println("NO");
else if(st.size() == 1)
System.out.println("YES");
else//2 different frequencies
{
int f1 = 0;
int f2 = 0;
int f1Count = 0;
int f2Count = 0;
int i = 0;
for(int n : st)
{
if(i == 0) f1 = n;
else f2 = n;
i++;
}
for(int freq : frequencies.values())
{
if(freq == f1) f1Count++;
if(freq == f2) f2Count++;
}
if((f1 == 1 && f1Count == 1 ) || (f2 == 1 && f2Count == 1 ))
System.out.println("YES");
else if ((Math.abs(f1 - f2) == 1) && (f1Count == 1 || f2Count == 1))
System.out.println("YES");
else
System.out.println("NO");
}
}
}