-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGameOfThrones1.java
More file actions
51 lines (39 loc) · 1.27 KB
/
GameOfThrones1.java
File metadata and controls
51 lines (39 loc) · 1.27 KB
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
// https://www.hackerrank.com/challenges/game-of-thrones
/*
0 - 9 = 48 - 57
A - Z = 65 - 90
a - z = 97 - 122
Initial Thought - Since the max length of string can be 10^5 which is large,
Count the characters and use counting sort techinque to find
all the characters numbers
Now, if every character is even then the string's anagram is palindrome
or if every character is even number and only one is 1, then also the string's anagram is palindrome
Time Complexity - O(n), n for parsing the string + 26 for parsing the countArr
Space Complexity - O(1) for arr of size 26
*/
import java.util.Scanner;
class GameOfThrones1 {
public static boolean checkPalindrome(int[] countArr) {
boolean palindrome = true;
for(int i = 0; i < 26; i++) {
if(countArr[i] % 2 == 1) {
if(!palindrome) {
return false;
}
palindrome = false;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
int len = s.length();
int[] countArr = new int[26];
for(int i = 0; i < len; i++) {
countArr[s.charAt(i) - 97]++;
}
String ans = checkPalindrome(countArr) ? "YES" : "NO";
System.out.println(ans);
}
}