Skip to content

Commit fe38bc4

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0266
1 parent 505c254 commit fe38bc4

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

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

+28-2
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,48 @@
2626

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

29+
利用 HashMap(字典表)统计每个字符出现的频率,至多有一个字符出现奇数次数即可。
30+
2931
<!-- tabs:start -->
3032

3133
### **Python3**
3234

3335
<!-- 这里可写当前语言的特殊实现逻辑 -->
3436

3537
```python
36-
38+
class Solution:
39+
def canPermutePalindrome(self, s: str) -> bool:
40+
mapper = {}
41+
for ch in s:
42+
mapper[ch] = mapper.get(ch, 0) + 1
43+
cnt = 0
44+
for _, v in mapper.items():
45+
if v % 2 != 0:
46+
cnt += 1
47+
return cnt <= 1
3748
```
3849

3950
### **Java**
4051

4152
<!-- 这里可写当前语言的特殊实现逻辑 -->
4253

4354
```java
44-
55+
class Solution {
56+
public boolean canPermutePalindrome(String s) {
57+
Map<Character, Integer> map = new HashMap<>();
58+
for (int i = 0, n = s.length(); i < n; ++i) {
59+
char ch = s.charAt(i);
60+
map.put(ch, map.getOrDefault(ch, 0) + 1);
61+
}
62+
int cnt = 0;
63+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
64+
if (entry.getValue() % 2 != 0) {
65+
++cnt;
66+
}
67+
}
68+
return cnt <= 1;
69+
}
70+
}
4571
```
4672

4773
### **...**

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,37 @@
3131
### **Python3**
3232

3333
```python
34-
34+
class Solution:
35+
def canPermutePalindrome(self, s: str) -> bool:
36+
mapper = {}
37+
for ch in s:
38+
mapper[ch] = mapper.get(ch, 0) + 1
39+
cnt = 0
40+
for _, v in mapper.items():
41+
if v % 2 != 0:
42+
cnt += 1
43+
return cnt <= 1
3544
```
3645

3746
### **Java**
3847

3948
```java
40-
49+
class Solution {
50+
public boolean canPermutePalindrome(String s) {
51+
Map<Character, Integer> map = new HashMap<>();
52+
for (int i = 0, n = s.length(); i < n; ++i) {
53+
char ch = s.charAt(i);
54+
map.put(ch, map.getOrDefault(ch, 0) + 1);
55+
}
56+
int cnt = 0;
57+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
58+
if (entry.getValue() % 2 != 0) {
59+
++cnt;
60+
}
61+
}
62+
return cnt <= 1;
63+
}
64+
}
4165
```
4266

4367
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean canPermutePalindrome(String s) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
for (int i = 0, n = s.length(); i < n; ++i) {
5+
char ch = s.charAt(i);
6+
map.put(ch, map.getOrDefault(ch, 0) + 1);
7+
}
8+
int cnt = 0;
9+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
10+
if (entry.getValue() % 2 != 0) {
11+
++cnt;
12+
}
13+
}
14+
return cnt <= 1;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def canPermutePalindrome(self, s: str) -> bool:
3+
mapper = {}
4+
for ch in s:
5+
mapper[ch] = mapper.get(ch, 0) + 1
6+
cnt = 0
7+
for _, v in mapper.items():
8+
if v % 2 != 0:
9+
cnt += 1
10+
return cnt <= 1

0 commit comments

Comments
 (0)