File tree Expand file tree Collapse file tree 4 files changed +42
-43
lines changed
lcci/01.04.Palindrome Permutation Expand file tree Collapse file tree 4 files changed +42
-43
lines changed Original file line number Diff line number Diff line change 3636``` python
3737class Solution :
3838 def canPermutePalindrome (self , s : str ) -> bool :
39- if s is None or len (s) < 2 :
40- return True
41- cache = {}
42- for ch in s:
43- cache[ch] = 1 if cache.get(ch) is None else cache[ch] + 1
39+ counter = collections.Counter()
40+ for c in s:
41+ counter[c] += 1
4442 cnt = 0
45- for k, v in cache.items ():
46- if (v & 1 ) == 1 :
43+ for val in counter.values ():
44+ if (val & 1 ) == 1 :
4745 cnt += 1
4846 if cnt > 1 :
4947 return False
50- return cnt <= 1
48+ return True
5149```
5250
5351### ** Java**
@@ -57,24 +55,21 @@ class Solution:
5755``` java
5856class Solution {
5957 public boolean canPermutePalindrome (String s ) {
60- if (s == null || s. length() < 2 ) {
61- return true ;
62- }
63- char [] chars = s. toCharArray();
6458 Map<Character , Integer > counter = new HashMap<> ();
65- for (char ch : chars) {
66- counter. put(ch, counter. get(ch) == null ? 1 : counter. get(ch) + 1 );
59+ for (int i = 0 , n = s. length(); i < n; ++ i) {
60+ char c = s. charAt(i);
61+ counter. put(c, counter. getOrDefault(c, 0 ) + 1 );
6762 }
6863 int cnt = 0 ;
69- for (Map . Entry< Character , Integer > entry : counter. entrySet ()) {
70- if ((entry . getValue() & 1 ) == 1 ) {
64+ for (int val : counter. values ()) {
65+ if ((val & 1 ) == 1 ) {
7166 ++ cnt;
7267 }
7368 if (cnt > 1 ) {
7469 return false ;
7570 }
7671 }
77- return cnt <= 1 ;
72+ return true ;
7873 }
7974}
8075```
Original file line number Diff line number Diff line change 2727``` python
2828class Solution :
2929 def canPermutePalindrome (self , s : str ) -> bool :
30- if s is None or len (s) < 2 :
31- return True
32- cache = {}
33- for ch in s:
34- cache[ch] = 1 if cache.get(ch) is None else cache[ch] + 1
30+ counter = collections.Counter()
31+ for c in s:
32+ counter[c] += 1
3533 cnt = 0
36- for k, v in cache.items ():
37- if (v & 1 ) == 1 :
34+ for val in counter.values ():
35+ if (val & 1 ) == 1 :
3836 cnt += 1
3937 if cnt > 1 :
4038 return False
41- return cnt <= 1
39+ return True
4240```
4341
4442### ** Java**
4543
4644``` java
4745class Solution {
4846 public boolean canPermutePalindrome (String s ) {
49- if (s == null || s. length() < 2 ) {
50- return true ;
51- }
52- char [] chars = s. toCharArray();
5347 Map<Character , Integer > counter = new HashMap<> ();
54- for (char ch : chars) {
55- counter. put(ch, counter. get(ch) == null ? 1 : counter. get(ch) + 1 );
48+ for (int i = 0 , n = s. length(); i < n; ++ i) {
49+ char c = s. charAt(i);
50+ counter. put(c, counter. getOrDefault(c, 0 ) + 1 );
5651 }
5752 int cnt = 0 ;
58- for (Map . Entry< Character , Integer > entry : counter. entrySet ()) {
59- if ((entry . getValue() & 1 ) == 1 ) {
53+ for (int val : counter. values ()) {
54+ if ((val & 1 ) == 1 ) {
6055 ++ cnt;
6156 }
6257 if (cnt > 1 ) {
6358 return false ;
6459 }
6560 }
66- return cnt <= 1 ;
61+ return true ;
6762 }
6863}
6964```
Original file line number Diff line number Diff line change 11class Solution {
22 public boolean canPermutePalindrome (String s ) {
3- if (s == null || s .length () < 2 ) {
4- return true ;
5- }
6- char [] chars = s .toCharArray ();
73 Map <Character , Integer > counter = new HashMap <>();
8- for (char ch : chars ) {
9- counter .put (ch , counter .get (ch ) == null ? 1 : counter .get (ch ) + 1 );
4+ for (int i = 0 , n = s .length (); i < n ; ++i ) {
5+ char c = s .charAt (i );
6+ counter .put (c , counter .getOrDefault (c , 0 ) + 1 );
107 }
118 int cnt = 0 ;
12- for (Map . Entry < Character , Integer > entry : counter .entrySet ()) {
13- if ((entry . getValue () & 1 ) == 1 ) {
9+ for (int val : counter .values ()) {
10+ if ((val & 1 ) == 1 ) {
1411 ++cnt ;
1512 }
1613 if (cnt > 1 ) {
1714 return false ;
1815 }
1916 }
20- return cnt <= 1 ;
17+ return true ;
2118 }
2219}
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def canPermutePalindrome (self , s : str ) -> bool :
3+ counter = collections .Counter ()
4+ for c in s :
5+ counter [c ] += 1
6+ cnt = 0
7+ for val in counter .values ():
8+ if (val & 1 ) == 1 :
9+ cnt += 1
10+ if cnt > 1 :
11+ return False
12+ return True
You can’t perform that action at this time.
0 commit comments