Skip to content

Commit 80fdd6f

Browse files
committed
feat: update solutions to lc problem: No.0266
No.0266.Palindrome Permutation
1 parent 825f94b commit 80fdd6f

File tree

5 files changed

+48
-44
lines changed

5 files changed

+48
-44
lines changed

solution/0200-0299/0266.Palindrome Permutation/README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class Solution:
4949
```java
5050
class Solution {
5151
public boolean canPermutePalindrome(String s) {
52-
Map<Character, Integer> counter = new HashMap<>();
52+
int[] counter = new int[26];
5353
for (char c : s.toCharArray()) {
54-
counter.put(c, counter.getOrDefault(c, 0) + 1);
54+
++counter[c - 'a'];
5555
}
5656
int oddCnt = 0;
57-
for (int e : counter.values()) {
58-
oddCnt += e % 2;
57+
for (int cnt : counter) {
58+
oddCnt += cnt % 2;
5959
}
6060
return oddCnt < 2;
6161
}
@@ -68,10 +68,10 @@ class Solution {
6868
class Solution {
6969
public:
7070
bool canPermutePalindrome(string s) {
71-
unordered_map<char, int> counter;
72-
for (char c : s) ++counter[c];
71+
vector<int> counter(26);
72+
for (auto& c : s) ++counter[c - 'a'];
7373
int oddCnt = 0;
74-
for (auto& it : counter) oddCnt += it.second % 2;
74+
for (int& cnt : counter) oddCnt += cnt % 2;
7575
return oddCnt < 2;
7676
}
7777
};
@@ -81,15 +81,15 @@ public:
8181
8282
```go
8383
func canPermutePalindrome(s string) bool {
84-
counter := make(map[rune]int)
85-
for _, c := range s {
86-
counter[c]++
87-
}
88-
oddCnt := 0
89-
for _, e := range counter {
90-
oddCnt += e % 2
91-
}
92-
return oddCnt < 2
84+
counter := make([]int, 26)
85+
for i := range s {
86+
counter[s[i]-'a']++
87+
}
88+
oddCnt := 0
89+
for _, cnt := range counter {
90+
oddCnt += cnt % 2
91+
}
92+
return oddCnt < 2
9393
}
9494
```
9595

solution/0200-0299/0266.Palindrome Permutation/README_EN.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ class Solution:
5454
```java
5555
class Solution {
5656
public boolean canPermutePalindrome(String s) {
57-
Map<Character, Integer> counter = new HashMap<>();
57+
int[] counter = new int[26];
5858
for (char c : s.toCharArray()) {
59-
counter.put(c, counter.getOrDefault(c, 0) + 1);
59+
++counter[c - 'a'];
6060
}
6161
int oddCnt = 0;
62-
for (int e : counter.values()) {
63-
oddCnt += e % 2;
62+
for (int cnt : counter) {
63+
oddCnt += cnt % 2;
6464
}
6565
return oddCnt < 2;
6666
}
@@ -73,10 +73,10 @@ class Solution {
7373
class Solution {
7474
public:
7575
bool canPermutePalindrome(string s) {
76-
unordered_map<char, int> counter;
77-
for (char c : s) ++counter[c];
76+
vector<int> counter(26);
77+
for (auto& c : s) ++counter[c - 'a'];
7878
int oddCnt = 0;
79-
for (auto& it : counter) oddCnt += it.second % 2;
79+
for (int& cnt : counter) oddCnt += cnt % 2;
8080
return oddCnt < 2;
8181
}
8282
};
@@ -86,15 +86,15 @@ public:
8686
8787
```go
8888
func canPermutePalindrome(s string) bool {
89-
counter := make(map[rune]int)
90-
for _, c := range s {
91-
counter[c]++
92-
}
93-
oddCnt := 0
94-
for _, e := range counter {
95-
oddCnt += e % 2
96-
}
97-
return oddCnt < 2
89+
counter := make([]int, 26)
90+
for i := range s {
91+
counter[s[i]-'a']++
92+
}
93+
oddCnt := 0
94+
for _, cnt := range counter {
95+
oddCnt += cnt % 2
96+
}
97+
return oddCnt < 2
9898
}
9999
```
100100

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
class Solution {
22
public:
33
bool canPermutePalindrome(string s) {
4-
unordered_map<char, int> counter;
5-
for (char c : s) ++counter[c];
4+
vector<int> counter(26);
5+
for (auto& c : s) ++counter[c - 'a'];
66
int oddCnt = 0;
7-
for (auto& it : counter) oddCnt += it.second % 2;
7+
for (int& cnt : counter)
8+
if (cnt % 2 == 1)
9+
++oddCnt;
810
return oddCnt < 2;
911
}
1012
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
func canPermutePalindrome(s string) bool {
2-
counter := make(map[rune]int)
3-
for _, c := range s {
4-
counter[c]++
2+
counter := make([]int, 26)
3+
for i := range s {
4+
counter[s[i]-'a']++
55
}
66
oddCnt := 0
7-
for _, e := range counter {
8-
oddCnt += e % 2
7+
for _, cnt := range counter {
8+
oddCnt += cnt % 2
99
}
1010
return oddCnt < 2
1111
}

solution/0200-0299/0266.Palindrome Permutation/Solution.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class Solution {
22
public boolean canPermutePalindrome(String s) {
3-
Map<Character, Integer> counter = new HashMap<>();
3+
int[] counter = new int[26];
44
for (char c : s.toCharArray()) {
5-
counter.put(c, counter.getOrDefault(c, 0) + 1);
5+
++counter[c - 'a'];
66
}
77
int oddCnt = 0;
8-
for (int e : counter.values()) {
9-
oddCnt += e % 2;
8+
for (int cnt : counter) {
9+
if (cnt % 2 == 1) {
10+
++oddCnt;
11+
}
1012
}
1113
return oddCnt < 2;
1214
}

0 commit comments

Comments
 (0)