-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromPermutation.java
56 lines (48 loc) · 1.34 KB
/
PalindromPermutation.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
// LeettCode 266 -Given a string, determine if a permutation of the string could form a palindrome.
//
// Example 1:
//
// Input: "code"
// Output: false
// Example 2:
//
// Input: "aab"
// Output: true
// Example 3:
//
// Input: "carerac"
// Output: true
import java.util.HashMap;
//Time Complexity O(n) and space complexity O(n)
public class PalindromPermutation {
public static boolean bruteForce(String s){
if(s.length < 1) return true;
boolean isPalindromPermutation = true;
int len = s.length();
HashMap<Character, Integer> charMap = new HashMap<>();
for(int i = 0; i < s.length(); i++){
Character c = s.charAt(i);
if(charMap.containsKey(c)){
charMap.put(c, charMap.get(c) + 1);
} else {
charMap.put(c, 1);
}
}
int numOfOnes = 0;
int currentOccurence = 0;
for(Character a: charMap.keySet()){
currentOccurence = charMap.get(a);
if(currentOccurence % 2 == 1){
numOfOnes += 1;
}
if(numOfOnes > 1){return false; }
}
return isPalindromPermutation;
}
public static void main(String[] args){
String s1 = "code", s2 = "aab", s3 ="carerac";
System.out.println(bruteForce(s1));
System.out.println(bruteForce(s2));
System.out.println(bruteForce(s3));
}
}