diff --git a/lcci/01.01.Is Unique/README.md b/lcci/01.01.Is Unique/README.md
index e8f0aafbf7165..013967f04e16f 100644
--- a/lcci/01.01.Is Unique/README.md
+++ b/lcci/01.01.Is Unique/README.md
@@ -19,7 +19,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/01.01.Is%20Unique/REA
示例 1:
输入: s = "leetcode"
-输出: false
+输出: false
示例 2:
@@ -56,8 +56,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/01.01.Is%20Unique/REA
class Solution:
def isUnique(self, astr: str) -> bool:
mask = 0
- for c in astr:
- i = ord(c) - ord('a')
+ for i in map(lambda c: ord(c) - ord("a"), astr):
if (mask >> i) & 1:
return False
mask |= 1 << i
diff --git a/lcci/01.01.Is Unique/README_EN.md b/lcci/01.01.Is Unique/README_EN.md
index 86501a1e6e8e5..a63cf58656329 100644
--- a/lcci/01.01.Is Unique/README_EN.md
+++ b/lcci/01.01.Is Unique/README_EN.md
@@ -64,8 +64,7 @@ The time complexity is $O(n)$, where $n$ is the length of the string. The space
class Solution:
def isUnique(self, astr: str) -> bool:
mask = 0
- for c in astr:
- i = ord(c) - ord('a')
+ for i in map(lambda c: ord(c) - ord("a"), astr):
if (mask >> i) & 1:
return False
mask |= 1 << i
diff --git a/lcci/01.01.Is Unique/Solution.py b/lcci/01.01.Is Unique/Solution.py
index 1d63247d2b3a0..06482062b2939 100644
--- a/lcci/01.01.Is Unique/Solution.py
+++ b/lcci/01.01.Is Unique/Solution.py
@@ -1,8 +1,7 @@
class Solution:
def isUnique(self, astr: str) -> bool:
mask = 0
- for c in astr:
- i = ord(c) - ord('a')
+ for i in map(lambda c: ord(c) - ord("a"), astr):
if (mask >> i) & 1:
return False
mask |= 1 << i
diff --git a/lcci/01.02.Check Permutation/README.md b/lcci/01.02.Check Permutation/README.md
index db958faa9953e..786b5c7538b57 100644
--- a/lcci/01.02.Check Permutation/README.md
+++ b/lcci/01.02.Check Permutation/README.md
@@ -93,11 +93,18 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- if (s1.size() != s2.size()) return false;
- int cnt[26] = {0};
- for (char& c : s1) ++cnt[c - 'a'];
- for (char& c : s2)
- if (--cnt[c - 'a'] < 0) return false;
+ if (s1.size() != s2.size()) {
+ return false;
+ }
+ int cnt[26]{};
+ for (char c : s1) {
+ ++cnt[c - 'a'];
+ }
+ for (char c : s2) {
+ if (--cnt[c - 'a'] < 0) {
+ return false;
+ }
+ }
return true;
}
};
@@ -115,8 +122,7 @@ func CheckPermutation(s1 string, s2 string) bool {
cnt[c-'a']++
}
for _, c := range s2 {
- cnt[c-'a']--
- if cnt[c-'a'] < 0 {
+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
return false
}
}
@@ -128,20 +134,18 @@ func CheckPermutation(s1 string, s2 string) bool {
```ts
function CheckPermutation(s1: string, s2: string): boolean {
- const n = s1.length;
- const m = s2.length;
- if (n !== m) {
+ if (s1.length !== s2.length) {
return false;
}
- const map = new Map();
- for (let i = 0; i < n; i++) {
- map.set(s1[i], (map.get(s1[i]) ?? 0) + 1);
- map.set(s2[i], (map.get(s2[i]) ?? 0) - 1);
+ const cnt: Record = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (const v of map.values()) {
- if (v !== 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
}
@@ -150,22 +154,26 @@ function CheckPermutation(s1: string, s2: string): boolean {
#### Rust
```rust
-use std::collections::HashMap;
impl Solution {
pub fn check_permutation(s1: String, s2: String) -> bool {
- let n = s1.len();
- let m = s2.len();
- if n != m {
+ if s1.len() != s2.len() {
return false;
}
- let s1 = s1.as_bytes();
- let s2 = s2.as_bytes();
- let mut map = HashMap::new();
- for i in 0..n {
- *map.entry(s1[i]).or_insert(0) += 1;
- *map.entry(s2[i]).or_insert(0) -= 1;
+
+ let mut cnt = vec![0; 26];
+ for c in s1.chars() {
+ cnt[(c as usize - 'a' as usize)] += 1;
+ }
+
+ for c in s2.chars() {
+ let index = c as usize - 'a' as usize;
+ if cnt[index] == 0 {
+ return false;
+ }
+ cnt[index] -= 1;
}
- map.values().all(|i| *i == 0)
+
+ true
}
}
```
@@ -179,19 +187,18 @@ impl Solution {
* @return {boolean}
*/
var CheckPermutation = function (s1, s2) {
- if (s1.length != s2.length) {
+ if (s1.length !== s2.length) {
return false;
}
- const cnt = new Array(26).fill(0);
- for (let i = 0; i < s1.length; ++i) {
- const j = s1.codePointAt(i) - 'a'.codePointAt(0);
- ++cnt[j];
+ const cnt = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (let i = 0; i < s2.length; ++i) {
- const j = s2.codePointAt(i) - 'a'.codePointAt(0);
- if (--cnt[j] < 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
};
@@ -206,19 +213,18 @@ class Solution {
return false
}
- var cnt = Array(repeating: 0, count: 26)
+ var cnt = [Int](repeating: 0, count: 26)
for char in s1 {
- let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] += 1
+ cnt[Int(char.asciiValue! - Character("a").asciiValue!)] += 1
}
for char in s2 {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] -= 1
- if cnt[index] < 0 {
+ if cnt[index] == 0 {
return false
}
+ cnt[index] -= 1
}
return true
@@ -268,8 +274,8 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- sort(s1.begin(), s1.end());
- sort(s2.begin(), s2.end());
+ ranges::sort(s1);
+ ranges::sort(s2);
return s1 == s2;
}
};
@@ -308,6 +314,31 @@ impl Solution {
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {string} s1
+ * @param {string} s2
+ * @return {boolean}
+ */
+var CheckPermutation = function (s1, s2) {
+ return [...s1].sort().join('') === [...s2].sort().join('');
+};
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func CheckPermutation(_ s1: String, _ s2: String) -> Bool {
+ let s1 = s1.sorted()
+ let s2 = s2.sorted()
+ return s1 == s2
+ }
+}
+```
+
diff --git a/lcci/01.02.Check Permutation/README_EN.md b/lcci/01.02.Check Permutation/README_EN.md
index c18cd58477e8d..c582f9a548145 100644
--- a/lcci/01.02.Check Permutation/README_EN.md
+++ b/lcci/01.02.Check Permutation/README_EN.md
@@ -100,11 +100,18 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- if (s1.size() != s2.size()) return false;
- int cnt[26] = {0};
- for (char& c : s1) ++cnt[c - 'a'];
- for (char& c : s2)
- if (--cnt[c - 'a'] < 0) return false;
+ if (s1.size() != s2.size()) {
+ return false;
+ }
+ int cnt[26]{};
+ for (char c : s1) {
+ ++cnt[c - 'a'];
+ }
+ for (char c : s2) {
+ if (--cnt[c - 'a'] < 0) {
+ return false;
+ }
+ }
return true;
}
};
@@ -122,8 +129,7 @@ func CheckPermutation(s1 string, s2 string) bool {
cnt[c-'a']++
}
for _, c := range s2 {
- cnt[c-'a']--
- if cnt[c-'a'] < 0 {
+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
return false
}
}
@@ -135,20 +141,18 @@ func CheckPermutation(s1 string, s2 string) bool {
```ts
function CheckPermutation(s1: string, s2: string): boolean {
- const n = s1.length;
- const m = s2.length;
- if (n !== m) {
+ if (s1.length !== s2.length) {
return false;
}
- const map = new Map();
- for (let i = 0; i < n; i++) {
- map.set(s1[i], (map.get(s1[i]) ?? 0) + 1);
- map.set(s2[i], (map.get(s2[i]) ?? 0) - 1);
+ const cnt: Record = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (const v of map.values()) {
- if (v !== 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
}
@@ -157,22 +161,26 @@ function CheckPermutation(s1: string, s2: string): boolean {
#### Rust
```rust
-use std::collections::HashMap;
impl Solution {
pub fn check_permutation(s1: String, s2: String) -> bool {
- let n = s1.len();
- let m = s2.len();
- if n != m {
+ if s1.len() != s2.len() {
return false;
}
- let s1 = s1.as_bytes();
- let s2 = s2.as_bytes();
- let mut map = HashMap::new();
- for i in 0..n {
- *map.entry(s1[i]).or_insert(0) += 1;
- *map.entry(s2[i]).or_insert(0) -= 1;
+
+ let mut cnt = vec![0; 26];
+ for c in s1.chars() {
+ cnt[(c as usize - 'a' as usize)] += 1;
+ }
+
+ for c in s2.chars() {
+ let index = c as usize - 'a' as usize;
+ if cnt[index] == 0 {
+ return false;
+ }
+ cnt[index] -= 1;
}
- map.values().all(|i| *i == 0)
+
+ true
}
}
```
@@ -186,19 +194,18 @@ impl Solution {
* @return {boolean}
*/
var CheckPermutation = function (s1, s2) {
- if (s1.length != s2.length) {
+ if (s1.length !== s2.length) {
return false;
}
- const cnt = new Array(26).fill(0);
- for (let i = 0; i < s1.length; ++i) {
- const j = s1.codePointAt(i) - 'a'.codePointAt(0);
- ++cnt[j];
+ const cnt = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (let i = 0; i < s2.length; ++i) {
- const j = s2.codePointAt(i) - 'a'.codePointAt(0);
- if (--cnt[j] < 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
};
@@ -213,19 +220,18 @@ class Solution {
return false
}
- var cnt = Array(repeating: 0, count: 26)
+ var cnt = [Int](repeating: 0, count: 26)
for char in s1 {
- let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] += 1
+ cnt[Int(char.asciiValue! - Character("a").asciiValue!)] += 1
}
for char in s2 {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] -= 1
- if cnt[index] < 0 {
+ if cnt[index] == 0 {
return false
}
+ cnt[index] -= 1
}
return true
@@ -275,8 +281,8 @@ class Solution {
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- sort(s1.begin(), s1.end());
- sort(s2.begin(), s2.end());
+ ranges::sort(s1);
+ ranges::sort(s2);
return s1 == s2;
}
};
@@ -315,6 +321,31 @@ impl Solution {
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {string} s1
+ * @param {string} s2
+ * @return {boolean}
+ */
+var CheckPermutation = function (s1, s2) {
+ return [...s1].sort().join('') === [...s2].sort().join('');
+};
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func CheckPermutation(_ s1: String, _ s2: String) -> Bool {
+ let s1 = s1.sorted()
+ let s2 = s2.sorted()
+ return s1 == s2
+ }
+}
+```
+
diff --git a/lcci/01.02.Check Permutation/Solution.cpp b/lcci/01.02.Check Permutation/Solution.cpp
index 72808b7ee740c..ff8e6d531810c 100644
--- a/lcci/01.02.Check Permutation/Solution.cpp
+++ b/lcci/01.02.Check Permutation/Solution.cpp
@@ -1,11 +1,18 @@
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- if (s1.size() != s2.size()) return false;
- int cnt[26] = {0};
- for (char& c : s1) ++cnt[c - 'a'];
- for (char& c : s2)
- if (--cnt[c - 'a'] < 0) return false;
+ if (s1.size() != s2.size()) {
+ return false;
+ }
+ int cnt[26]{};
+ for (char c : s1) {
+ ++cnt[c - 'a'];
+ }
+ for (char c : s2) {
+ if (--cnt[c - 'a'] < 0) {
+ return false;
+ }
+ }
return true;
}
-};
\ No newline at end of file
+};
diff --git a/lcci/01.02.Check Permutation/Solution.go b/lcci/01.02.Check Permutation/Solution.go
index 7acd2c2c4bc00..e8fd1ace061de 100644
--- a/lcci/01.02.Check Permutation/Solution.go
+++ b/lcci/01.02.Check Permutation/Solution.go
@@ -7,10 +7,9 @@ func CheckPermutation(s1 string, s2 string) bool {
cnt[c-'a']++
}
for _, c := range s2 {
- cnt[c-'a']--
- if cnt[c-'a'] < 0 {
+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
return false
}
}
return true
-}
\ No newline at end of file
+}
diff --git a/lcci/01.02.Check Permutation/Solution.js b/lcci/01.02.Check Permutation/Solution.js
index 4c0adedaf125b..7aa0ea9b1d428 100644
--- a/lcci/01.02.Check Permutation/Solution.js
+++ b/lcci/01.02.Check Permutation/Solution.js
@@ -4,19 +4,18 @@
* @return {boolean}
*/
var CheckPermutation = function (s1, s2) {
- if (s1.length != s2.length) {
+ if (s1.length !== s2.length) {
return false;
}
- const cnt = new Array(26).fill(0);
- for (let i = 0; i < s1.length; ++i) {
- const j = s1.codePointAt(i) - 'a'.codePointAt(0);
- ++cnt[j];
+ const cnt = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (let i = 0; i < s2.length; ++i) {
- const j = s2.codePointAt(i) - 'a'.codePointAt(0);
- if (--cnt[j] < 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
};
diff --git a/lcci/01.02.Check Permutation/Solution.rs b/lcci/01.02.Check Permutation/Solution.rs
index efe963a0130f2..45cda06875452 100644
--- a/lcci/01.02.Check Permutation/Solution.rs
+++ b/lcci/01.02.Check Permutation/Solution.rs
@@ -1,18 +1,22 @@
-use std::collections::HashMap;
impl Solution {
pub fn check_permutation(s1: String, s2: String) -> bool {
- let n = s1.len();
- let m = s2.len();
- if n != m {
+ if s1.len() != s2.len() {
return false;
}
- let s1 = s1.as_bytes();
- let s2 = s2.as_bytes();
- let mut map = HashMap::new();
- for i in 0..n {
- *map.entry(s1[i]).or_insert(0) += 1;
- *map.entry(s2[i]).or_insert(0) -= 1;
+
+ let mut cnt = vec![0; 26];
+ for c in s1.chars() {
+ cnt[(c as usize - 'a' as usize)] += 1;
}
- map.values().all(|i| *i == 0)
+
+ for c in s2.chars() {
+ let index = c as usize - 'a' as usize;
+ if cnt[index] == 0 {
+ return false;
+ }
+ cnt[index] -= 1;
+ }
+
+ true
}
}
diff --git a/lcci/01.02.Check Permutation/Solution.swift b/lcci/01.02.Check Permutation/Solution.swift
index 7161edfad155c..6bfd2af7416a5 100644
--- a/lcci/01.02.Check Permutation/Solution.swift
+++ b/lcci/01.02.Check Permutation/Solution.swift
@@ -3,22 +3,21 @@ class Solution {
if s1.count != s2.count {
return false
}
-
- var cnt = Array(repeating: 0, count: 26)
-
+
+ var cnt = [Int](repeating: 0, count: 26)
+
for char in s1 {
- let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] += 1
+ cnt[Int(char.asciiValue! - Character("a").asciiValue!)] += 1
}
-
+
for char in s2 {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
- cnt[index] -= 1
- if cnt[index] < 0 {
+ if cnt[index] == 0 {
return false
}
+ cnt[index] -= 1
}
-
+
return true
}
-}
\ No newline at end of file
+}
diff --git a/lcci/01.02.Check Permutation/Solution.ts b/lcci/01.02.Check Permutation/Solution.ts
index 65f0c434d61b9..b6bb1f8822cec 100644
--- a/lcci/01.02.Check Permutation/Solution.ts
+++ b/lcci/01.02.Check Permutation/Solution.ts
@@ -1,18 +1,16 @@
function CheckPermutation(s1: string, s2: string): boolean {
- const n = s1.length;
- const m = s2.length;
- if (n !== m) {
+ if (s1.length !== s2.length) {
return false;
}
- const map = new Map();
- for (let i = 0; i < n; i++) {
- map.set(s1[i], (map.get(s1[i]) ?? 0) + 1);
- map.set(s2[i], (map.get(s2[i]) ?? 0) - 1);
+ const cnt: Record = {};
+ for (const c of s1) {
+ cnt[c] = (cnt[c] || 0) + 1;
}
- for (const v of map.values()) {
- if (v !== 0) {
+ for (const c of s2) {
+ if (!cnt[c]) {
return false;
}
+ cnt[c]--;
}
return true;
}
diff --git a/lcci/01.02.Check Permutation/Solution2.cpp b/lcci/01.02.Check Permutation/Solution2.cpp
index 70c67c68db593..142e3154d5a17 100644
--- a/lcci/01.02.Check Permutation/Solution2.cpp
+++ b/lcci/01.02.Check Permutation/Solution2.cpp
@@ -1,8 +1,8 @@
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
- sort(s1.begin(), s1.end());
- sort(s2.begin(), s2.end());
+ ranges::sort(s1);
+ ranges::sort(s2);
return s1 == s2;
}
-};
\ No newline at end of file
+};
diff --git a/lcci/01.02.Check Permutation/Solution2.js b/lcci/01.02.Check Permutation/Solution2.js
new file mode 100644
index 0000000000000..a5d33c913da3a
--- /dev/null
+++ b/lcci/01.02.Check Permutation/Solution2.js
@@ -0,0 +1,8 @@
+/**
+ * @param {string} s1
+ * @param {string} s2
+ * @return {boolean}
+ */
+var CheckPermutation = function (s1, s2) {
+ return [...s1].sort().join('') === [...s2].sort().join('');
+};
diff --git a/lcci/01.02.Check Permutation/Solution2.swift b/lcci/01.02.Check Permutation/Solution2.swift
new file mode 100644
index 0000000000000..0aef38caddbb4
--- /dev/null
+++ b/lcci/01.02.Check Permutation/Solution2.swift
@@ -0,0 +1,7 @@
+class Solution {
+ func CheckPermutation(_ s1: String, _ s2: String) -> Bool {
+ let s1 = s1.sorted()
+ let s2 = s2.sorted()
+ return s1 == s2
+ }
+}
diff --git a/lcci/01.04.Palindrome Permutation/README.md b/lcci/01.04.Palindrome Permutation/README.md
index 689ccb5841266..b174846e8cd30 100644
--- a/lcci/01.04.Palindrome Permutation/README.md
+++ b/lcci/01.04.Palindrome Permutation/README.md
@@ -94,18 +94,15 @@ public:
```go
func canPermutePalindrome(s string) bool {
- vis := map[rune]bool{}
- cnt := 0
+ cnt := map[rune]int{}
for _, c := range s {
- if vis[c] {
- vis[c] = false
- cnt--
- } else {
- vis[c] = true
- cnt++
- }
+ cnt[c]++
}
- return cnt < 2
+ sum := 0
+ for _, v := range cnt {
+ sum += v & 1
+ }
+ return sum < 2
}
```
@@ -113,34 +110,26 @@ func canPermutePalindrome(s string) bool {
```ts
function canPermutePalindrome(s: string): boolean {
- const set = new Set();
+ const cnt: Record = {};
for (const c of s) {
- if (set.has(c)) {
- set.delete(c);
- } else {
- set.add(c);
- }
+ cnt[c] = (cnt[c] || 0) + 1;
}
- return set.size <= 1;
+ return Object.values(cnt).filter(v => v % 2 === 1).length < 2;
}
```
#### Rust
```rust
-use std::collections::HashSet;
+use std::collections::HashMap;
impl Solution {
pub fn can_permute_palindrome(s: String) -> bool {
- let mut set = HashSet::new();
+ let mut cnt = HashMap::new();
for c in s.chars() {
- if set.contains(&c) {
- set.remove(&c);
- } else {
- set.insert(c);
- }
+ *cnt.entry(c).or_insert(0) += 1;
}
- set.len() <= 1
+ cnt.values().filter(|&&v| v % 2 == 1).count() < 2
}
}
```
@@ -173,7 +162,7 @@ class Solution {
### 方法二:哈希表的另一种实现
-我们用哈希表 $vis$ 存储每个字符是否出现过。若出现过,则从哈希表中删除该字符;否则,将该字符加入哈希表。
+我们用一个哈希表 $\textit{vis}$ 存储每个字符是否出现过。若出现过,则从哈希表中删除该字符;否则,将该字符加入哈希表。
最后判断哈希表中字符的个数是否小于 $2$,若是,则是回文排列。
@@ -231,6 +220,76 @@ public:
};
```
+#### Go
+
+```go
+func canPermutePalindrome(s string) bool {
+ vis := map[rune]bool{}
+ for _, c := range s {
+ if vis[c] {
+ delete(vis, c)
+ } else {
+ vis[c] = true
+ }
+ }
+ return len(vis) < 2
+}
+```
+
+#### TypeScript
+
+```ts
+function canPermutePalindrome(s: string): boolean {
+ const vis = new Set();
+ for (const c of s) {
+ if (vis.has(c)) {
+ vis.delete(c);
+ } else {
+ vis.add(c);
+ }
+ }
+ return vis.size < 2;
+}
+```
+
+#### Rust
+
+```rust
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn can_permute_palindrome(s: String) -> bool {
+ let mut vis = HashSet::new();
+ for c in s.chars() {
+ if vis.contains(&c) {
+ vis.remove(&c);
+ } else {
+ vis.insert(c);
+ }
+ }
+ vis.len() < 2
+ }
+}
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func canPermutePalindrome(_ s: String) -> Bool {
+ var vis = Set()
+ for c in s {
+ if vis.contains(c) {
+ vis.remove(c)
+ } else {
+ vis.insert(c)
+ }
+ }
+ return vis.count < 2
+ }
+}
+```
+
diff --git a/lcci/01.04.Palindrome Permutation/README_EN.md b/lcci/01.04.Palindrome Permutation/README_EN.md
index e942aadc8b522..28d7e6c7c4c02 100644
--- a/lcci/01.04.Palindrome Permutation/README_EN.md
+++ b/lcci/01.04.Palindrome Permutation/README_EN.md
@@ -92,18 +92,15 @@ public:
```go
func canPermutePalindrome(s string) bool {
- vis := map[rune]bool{}
- cnt := 0
+ cnt := map[rune]int{}
for _, c := range s {
- if vis[c] {
- vis[c] = false
- cnt--
- } else {
- vis[c] = true
- cnt++
- }
+ cnt[c]++
}
- return cnt < 2
+ sum := 0
+ for _, v := range cnt {
+ sum += v & 1
+ }
+ return sum < 2
}
```
@@ -111,34 +108,26 @@ func canPermutePalindrome(s string) bool {
```ts
function canPermutePalindrome(s: string): boolean {
- const set = new Set();
+ const cnt: Record = {};
for (const c of s) {
- if (set.has(c)) {
- set.delete(c);
- } else {
- set.add(c);
- }
+ cnt[c] = (cnt[c] || 0) + 1;
}
- return set.size <= 1;
+ return Object.values(cnt).filter(v => v % 2 === 1).length < 2;
}
```
#### Rust
```rust
-use std::collections::HashSet;
+use std::collections::HashMap;
impl Solution {
pub fn can_permute_palindrome(s: String) -> bool {
- let mut set = HashSet::new();
+ let mut cnt = HashMap::new();
for c in s.chars() {
- if set.contains(&c) {
- set.remove(&c);
- } else {
- set.insert(c);
- }
+ *cnt.entry(c).or_insert(0) += 1;
}
- set.len() <= 1
+ cnt.values().filter(|&&v| v % 2 == 1).count() < 2
}
}
```
@@ -229,6 +218,76 @@ public:
};
```
+#### Go
+
+```go
+func canPermutePalindrome(s string) bool {
+ vis := map[rune]bool{}
+ for _, c := range s {
+ if vis[c] {
+ delete(vis, c)
+ } else {
+ vis[c] = true
+ }
+ }
+ return len(vis) < 2
+}
+```
+
+#### TypeScript
+
+```ts
+function canPermutePalindrome(s: string): boolean {
+ const vis = new Set();
+ for (const c of s) {
+ if (vis.has(c)) {
+ vis.delete(c);
+ } else {
+ vis.add(c);
+ }
+ }
+ return vis.size < 2;
+}
+```
+
+#### Rust
+
+```rust
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn can_permute_palindrome(s: String) -> bool {
+ let mut vis = HashSet::new();
+ for c in s.chars() {
+ if vis.contains(&c) {
+ vis.remove(&c);
+ } else {
+ vis.insert(c);
+ }
+ }
+ vis.len() < 2
+ }
+}
+```
+
+#### Swift
+
+```swift
+class Solution {
+ func canPermutePalindrome(_ s: String) -> Bool {
+ var vis = Set()
+ for c in s {
+ if vis.contains(c) {
+ vis.remove(c)
+ } else {
+ vis.insert(c)
+ }
+ }
+ return vis.count < 2
+ }
+}
+```
+
diff --git a/lcci/01.04.Palindrome Permutation/Solution.go b/lcci/01.04.Palindrome Permutation/Solution.go
index 5d156fd33340e..76d5c69a60a52 100644
--- a/lcci/01.04.Palindrome Permutation/Solution.go
+++ b/lcci/01.04.Palindrome Permutation/Solution.go
@@ -1,14 +1,11 @@
-func canPermutePalindrome(s string) bool {
- vis := map[rune]bool{}
- cnt := 0
- for _, c := range s {
- if vis[c] {
- vis[c] = false
- cnt--
- } else {
- vis[c] = true
- cnt++
- }
- }
- return cnt < 2
-}
\ No newline at end of file
+func canPermutePalindrome(s string) bool {
+ cnt := map[rune]int{}
+ for _, c := range s {
+ cnt[c]++
+ }
+ sum := 0
+ for _, v := range cnt {
+ sum += v & 1
+ }
+ return sum < 2
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution.rs b/lcci/01.04.Palindrome Permutation/Solution.rs
index 0d0e1b07a5e70..895e119d6b604 100644
--- a/lcci/01.04.Palindrome Permutation/Solution.rs
+++ b/lcci/01.04.Palindrome Permutation/Solution.rs
@@ -1,15 +1,11 @@
-use std::collections::HashSet;
+use std::collections::HashMap;
impl Solution {
pub fn can_permute_palindrome(s: String) -> bool {
- let mut set = HashSet::new();
+ let mut cnt = HashMap::new();
for c in s.chars() {
- if set.contains(&c) {
- set.remove(&c);
- } else {
- set.insert(c);
- }
+ *cnt.entry(c).or_insert(0) += 1;
}
- set.len() <= 1
+ cnt.values().filter(|&&v| v % 2 == 1).count() < 2
}
}
diff --git a/lcci/01.04.Palindrome Permutation/Solution.ts b/lcci/01.04.Palindrome Permutation/Solution.ts
index 7bd7505057358..44ba2bc906e83 100644
--- a/lcci/01.04.Palindrome Permutation/Solution.ts
+++ b/lcci/01.04.Palindrome Permutation/Solution.ts
@@ -1,11 +1,7 @@
function canPermutePalindrome(s: string): boolean {
- const set = new Set();
+ const cnt: Record = {};
for (const c of s) {
- if (set.has(c)) {
- set.delete(c);
- } else {
- set.add(c);
- }
+ cnt[c] = (cnt[c] || 0) + 1;
}
- return set.size <= 1;
+ return Object.values(cnt).filter(v => v % 2 === 1).length < 2;
}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.go b/lcci/01.04.Palindrome Permutation/Solution2.go
new file mode 100644
index 0000000000000..425935647c9f4
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.go
@@ -0,0 +1,11 @@
+func canPermutePalindrome(s string) bool {
+ vis := map[rune]bool{}
+ for _, c := range s {
+ if vis[c] {
+ delete(vis, c)
+ } else {
+ vis[c] = true
+ }
+ }
+ return len(vis) < 2
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.rs b/lcci/01.04.Palindrome Permutation/Solution2.rs
new file mode 100644
index 0000000000000..5ef83e50f0c4d
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.rs
@@ -0,0 +1,15 @@
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn can_permute_palindrome(s: String) -> bool {
+ let mut vis = HashSet::new();
+ for c in s.chars() {
+ if vis.contains(&c) {
+ vis.remove(&c);
+ } else {
+ vis.insert(c);
+ }
+ }
+ vis.len() < 2
+ }
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.swift b/lcci/01.04.Palindrome Permutation/Solution2.swift
new file mode 100644
index 0000000000000..993dc395a5b33
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.swift
@@ -0,0 +1,13 @@
+class Solution {
+ func canPermutePalindrome(_ s: String) -> Bool {
+ var vis = Set()
+ for c in s {
+ if vis.contains(c) {
+ vis.remove(c)
+ } else {
+ vis.insert(c)
+ }
+ }
+ return vis.count < 2
+ }
+}
diff --git a/lcci/01.04.Palindrome Permutation/Solution2.ts b/lcci/01.04.Palindrome Permutation/Solution2.ts
new file mode 100644
index 0000000000000..c72db04c15438
--- /dev/null
+++ b/lcci/01.04.Palindrome Permutation/Solution2.ts
@@ -0,0 +1,11 @@
+function canPermutePalindrome(s: string): boolean {
+ const vis = new Set();
+ for (const c of s) {
+ if (vis.has(c)) {
+ vis.delete(c);
+ } else {
+ vis.add(c);
+ }
+ }
+ return vis.size < 2;
+}