-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.java
90 lines (74 loc) · 1.64 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
//Problem: https://www.hackerrank.com/challenges/game-of-thrones
//Java 8
/*
A palindrome has the unique property that
the frequency of only one character can
be odd, so we can simply count the
frequency of characters and check if
there is more than 1 that is odd
Example:
aaabbbb
a: 3
b: 4
1 odd
cdefghmnopqrstuvw
c:1
d:1
e:1
f:1
g:1
h:1
m:1
n:1
o:1
p:1
q:1
r:1
s:1
t:1
u:1
v:1
w:1
18 odds
cdcdcdcdeeeef
c:4
d:4
e:4
f:1
1 odd
Time Complexity: O(n) //In the size of the string
Space Complexity: O(1) //We have a fixed number of different characters
*/
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> letters = new HashMap<>();
for(char c : s.toCharArray())
{
if(letters.containsKey(c))
letters.put(c, letters.get(c) + 1);
else
letters.put(c, 1);
}
int odd = 0;
int even = 0;
for(Integer frequency : letters.values())
{
if(frequency % 2 == 1)
{
odd++;
continue;
}
if(frequency % 2 == 0)
even++;
}
if(odd > 1)
System.out.println("NO");
else
System.out.println("YES");
}
}