Skip to content

Commit 3a35941

Browse files
refactor 266
1 parent e83503f commit 3a35941

File tree

1 file changed

+20
-29
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-29
lines changed

src/main/java/com/fishercoder/solutions/_266.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,31 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6-
/**Given a string, determine if a permutation of the string could form a palindrome.
7-
8-
For example,
9-
"code" -> False, "aab" -> True, "carerac" -> True.
10-
11-
Hint:
12-
13-
Consider the palindromes of odd vs even length. What difference do you notice?
14-
Count the frequency of each character.
15-
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?*/
166
public class _266 {
177

18-
public boolean canPermutePalindrome(String s) {
19-
20-
char[] chars = s.toCharArray();
21-
Map<Character, Integer> map = new HashMap<Character, Integer>();
22-
for (char c : chars) {
23-
if (!map.containsKey(c)) {
24-
map.put(c, 1);
25-
} else {
26-
map.put(c, map.get(c) + 1);
8+
public static class Solution1 {
9+
public boolean canPermutePalindrome(String s) {
10+
11+
char[] chars = s.toCharArray();
12+
Map<Character, Integer> map = new HashMap<>();
13+
for (char c : chars) {
14+
if (!map.containsKey(c)) {
15+
map.put(c, 1);
16+
} else {
17+
map.put(c, map.get(c) + 1);
18+
}
2719
}
28-
}
29-
int evenCount = 0;
30-
for (Map.Entry<Character, Integer> e : map.entrySet()) {
31-
if (e.getValue() % 2 != 0) {
32-
evenCount++;
33-
}
34-
if (evenCount > 1) {
35-
return false;
20+
int evenCount = 0;
21+
for (Map.Entry<Character, Integer> e : map.entrySet()) {
22+
if (e.getValue() % 2 != 0) {
23+
evenCount++;
24+
}
25+
if (evenCount > 1) {
26+
return false;
27+
}
3628
}
29+
return true;
3730
}
38-
return true;
39-
4031
}
4132

4233
}

0 commit comments

Comments
 (0)