Skip to content

Commit 0668924

Browse files
committed
feat: add solutions to lc problem: No.0266
No.0266.Palindrome Permutation
1 parent 7953aad commit 0668924

File tree

7 files changed

+125
-63
lines changed

7 files changed

+125
-63
lines changed

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

+52-21
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@
2727

2828
<!-- 这里可写通用的实现逻辑 -->
2929

30-
利用 HashMap(字典表)统计每个字符出现的频率,至多有一个字符出现奇数次数即可。
30+
**方法一:数组**
31+
32+
创建一个长度为 $26$ 的数组,统计每个字母出现的频率,至多有一个字符出现奇数次数即可。
33+
34+
时间复杂度 $O(n)$,空间复杂度 $O(26)$。其中 $n$ 是字符串的长度。
35+
36+
**方法二:哈希表**
37+
38+
利用哈希表来维护元素。遍历字符串每个字母 $s[i]$,若 $s[i]$ 在哈希表中,则将 $s[i]$ 从哈希表中删除,否则将 $s[i]$ 加入哈希表。
39+
40+
遍历结束,若哈希表中元素个数不超过 $1$,则返回 true,否则返回 false。
41+
42+
时间复杂度 $O(n)$,空间复杂度 $O(26)$。其中 $n$ 是字符串的长度。
3143

3244
<!-- tabs:start -->
3345

@@ -38,8 +50,7 @@
3850
```python
3951
class Solution:
4052
def canPermutePalindrome(self, s: str) -> bool:
41-
counter = Counter(s)
42-
return sum(e % 2 for e in counter.values()) < 2
53+
return sum(v % 2 for v in Counter(s).values()) <= 1
4354
```
4455

4556
### **Java**
@@ -49,15 +60,15 @@ class Solution:
4960
```java
5061
class Solution {
5162
public boolean canPermutePalindrome(String s) {
52-
int[] counter = new int[26];
63+
int[] cnt = new int[26];
5364
for (char c : s.toCharArray()) {
54-
++counter[c - 'a'];
65+
++cnt[c - 'a'];
5566
}
56-
int oddCnt = 0;
57-
for (int cnt : counter) {
58-
oddCnt += cnt % 2;
67+
int n = 0;
68+
for (int v : cnt) {
69+
n += v % 2;
5970
}
60-
return oddCnt < 2;
71+
return n < 2;
6172
}
6273
}
6374
```
@@ -68,11 +79,11 @@ class Solution {
6879
class Solution {
6980
public:
7081
bool canPermutePalindrome(string s) {
71-
vector<int> counter(26);
72-
for (auto& c : s) ++counter[c - 'a'];
73-
int oddCnt = 0;
74-
for (int& cnt : counter) oddCnt += cnt % 2;
75-
return oddCnt < 2;
82+
vector<int> cnt(26);
83+
for (char& c : s) ++cnt[c - 'a'];
84+
int n = 0;
85+
for (int& v : cnt) n += v & 1;
86+
return n < 2;
7687
}
7788
};
7889
```
@@ -81,18 +92,38 @@ public:
8192
8293
```go
8394
func canPermutePalindrome(s string) bool {
84-
counter := make([]int, 26)
85-
for i := range s {
86-
counter[s[i]-'a']++
95+
cnt := make([]int, 26)
96+
for _, c := range s {
97+
cnt[c-'a']++
8798
}
88-
oddCnt := 0
89-
for _, cnt := range counter {
90-
oddCnt += cnt % 2
99+
n := 0
100+
for _, v := range cnt {
101+
n += v & 1
91102
}
92-
return oddCnt < 2
103+
return n < 2
93104
}
94105
```
95106

107+
### **JavaScript**
108+
109+
```js
110+
/**
111+
* @param {string} s
112+
* @return {boolean}
113+
*/
114+
var canPermutePalindrome = function (s) {
115+
let ss = new Set();
116+
for (let c of s) {
117+
if (ss.has(c)) {
118+
ss.delete(c);
119+
} else {
120+
ss.add(c);
121+
}
122+
}
123+
return ss.size < 2;
124+
};
125+
```
126+
96127
### **...**
97128

98129
```

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

+39-20
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,23 @@
4545
```python
4646
class Solution:
4747
def canPermutePalindrome(self, s: str) -> bool:
48-
counter = Counter(s)
49-
return sum(e % 2 for e in counter.values()) < 2
48+
return sum(v % 2 for v in Counter(s).values()) <= 1
5049
```
5150

5251
### **Java**
5352

5453
```java
5554
class Solution {
5655
public boolean canPermutePalindrome(String s) {
57-
int[] counter = new int[26];
56+
int[] cnt = new int[26];
5857
for (char c : s.toCharArray()) {
59-
++counter[c - 'a'];
58+
++cnt[c - 'a'];
6059
}
61-
int oddCnt = 0;
62-
for (int cnt : counter) {
63-
oddCnt += cnt % 2;
60+
int n = 0;
61+
for (int v : cnt) {
62+
n += v % 2;
6463
}
65-
return oddCnt < 2;
64+
return n < 2;
6665
}
6766
}
6867
```
@@ -73,11 +72,11 @@ class Solution {
7372
class Solution {
7473
public:
7574
bool canPermutePalindrome(string s) {
76-
vector<int> counter(26);
77-
for (auto& c : s) ++counter[c - 'a'];
78-
int oddCnt = 0;
79-
for (int& cnt : counter) oddCnt += cnt % 2;
80-
return oddCnt < 2;
75+
vector<int> cnt(26);
76+
for (char& c : s) ++cnt[c - 'a'];
77+
int n = 0;
78+
for (int& v : cnt) n += v & 1;
79+
return n < 2;
8180
}
8281
};
8382
```
@@ -86,18 +85,38 @@ public:
8685
8786
```go
8887
func canPermutePalindrome(s string) bool {
89-
counter := make([]int, 26)
90-
for i := range s {
91-
counter[s[i]-'a']++
88+
cnt := make([]int, 26)
89+
for _, c := range s {
90+
cnt[c-'a']++
9291
}
93-
oddCnt := 0
94-
for _, cnt := range counter {
95-
oddCnt += cnt % 2
92+
n := 0
93+
for _, v := range cnt {
94+
n += v & 1
9695
}
97-
return oddCnt < 2
96+
return n < 2
9897
}
9998
```
10099

100+
### **JavaScript**
101+
102+
```js
103+
/**
104+
* @param {string} s
105+
* @return {boolean}
106+
*/
107+
var canPermutePalindrome = function (s) {
108+
let ss = new Set();
109+
for (let c of s) {
110+
if (ss.has(c)) {
111+
ss.delete(c);
112+
} else {
113+
ss.add(c);
114+
}
115+
}
116+
return ss.size < 2;
117+
};
118+
```
119+
101120
### **...**
102121

103122
```
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public:
33
bool canPermutePalindrome(string s) {
4-
vector<int> counter(26);
5-
for (auto& c : s) ++counter[c - 'a'];
6-
int oddCnt = 0;
7-
for (int& cnt : counter) oddCnt += cnt % 2;
8-
return oddCnt < 2;
4+
vector<int> cnt(26);
5+
for (char& c : s) ++cnt[c - 'a'];
6+
int n = 0;
7+
for (int& v : cnt) n += v & 1;
8+
return n < 2;
99
}
1010
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
func canPermutePalindrome(s string) bool {
2-
counter := make([]int, 26)
3-
for i := range s {
4-
counter[s[i]-'a']++
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
55
}
6-
oddCnt := 0
7-
for _, cnt := range counter {
8-
oddCnt += cnt % 2
6+
n := 0
7+
for _, v := range cnt {
8+
n += v & 1
99
}
10-
return oddCnt < 2
10+
return n < 2
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public boolean canPermutePalindrome(String s) {
3-
int[] counter = new int[26];
3+
int[] cnt = new int[26];
44
for (char c : s.toCharArray()) {
5-
++counter[c - 'a'];
5+
++cnt[c - 'a'];
66
}
7-
int oddCnt = 0;
8-
for (int cnt : counter) {
9-
if (cnt % 2 == 1) {
10-
++oddCnt;
11-
}
7+
int n = 0;
8+
for (int v : cnt) {
9+
n += v % 2;
1210
}
13-
return oddCnt < 2;
11+
return n < 2;
1412
}
1513
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var canPermutePalindrome = function (s) {
6+
let ss = new Set();
7+
for (let c of s) {
8+
if (ss.has(c)) {
9+
ss.delete(c);
10+
} else {
11+
ss.add(c);
12+
}
13+
}
14+
return ss.size < 2;
15+
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
class Solution:
22
def canPermutePalindrome(self, s: str) -> bool:
3-
counter = Counter(s)
4-
return sum(e % 2 for e in counter.values()) < 2
3+
return sum(v % 2 for v in Counter(s).values()) <= 1

0 commit comments

Comments
 (0)