-
Notifications
You must be signed in to change notification settings - Fork 80
/
SherlockAndValidString.java
60 lines (51 loc) · 1.46 KB
/
SherlockAndValidString.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
import java.util.HashMap;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Robert Nabil
*/
public class ValidString {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String s = sc.next();
HashMap<Character, Integer> h = new HashMap<Character, Integer>();
for (char i : s.toCharArray()) {
if (!h.containsKey(i)) {
h.put(i, 0);
}
h.put(i, h.get(i) + 1);
}
boolean isValid = true;
int n = h.get(s.charAt(0));
for (int i : h.values()) {
if (i != n) {
isValid = false;
}
}
if(isValid){
System.out.println("YES");
return;
}
for (int i = 0; i < s.length(); i++) {
isValid = true;
h.put(s.charAt(i), h.get(s.charAt(i)) - 1);
n = h.get(s.charAt(0));
for (int t : h.values()) {
if (t != n && t > 0) {
isValid = false;
}
}
h.put(s.charAt(i), h.get(s.charAt(i)) + 1);
if(isValid){
System.out.println("YES");
return;
}
}
System.out.println("NO");
}
}