Skip to content

Commit 9021c0a

Browse files
committed
feat: add solutions to lcci problem: No.01.04
No.01.04.Palindrome Permutation
1 parent 86d221a commit 9021c0a

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

lcci/01.04.Palindrome Permutation/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,42 @@ public:
9494
};
9595
```
9696
97+
### **TypeScript**
98+
99+
```ts
100+
function canPermutePalindrome(s: string): boolean {
101+
const set = new Set<string>();
102+
for (const c of s) {
103+
if (set.has(c)) {
104+
set.delete(c);
105+
} else {
106+
set.add(c);
107+
}
108+
}
109+
return set.size <= 1;
110+
}
111+
```
112+
113+
### **Rust**
114+
115+
```rust
116+
use std::collections::HashSet;
117+
118+
impl Solution {
119+
pub fn can_permute_palindrome(s: String) -> bool {
120+
let mut set = HashSet::new();
121+
for c in s.chars() {
122+
if set.contains(&c) {
123+
set.remove(&c);
124+
} else {
125+
set.insert(c);
126+
}
127+
}
128+
set.len() <= 1
129+
}
130+
}
131+
```
132+
97133
### **...**
98134

99135
```

lcci/01.04.Palindrome Permutation/README_EN.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,42 @@ public:
8383
};
8484
```
8585
86+
### **TypeScript**
87+
88+
```ts
89+
function canPermutePalindrome(s: string): boolean {
90+
const set = new Set<string>();
91+
for (const c of s) {
92+
if (set.has(c)) {
93+
set.delete(c);
94+
} else {
95+
set.add(c);
96+
}
97+
}
98+
return set.size <= 1;
99+
}
100+
```
101+
102+
### **Rust**
103+
104+
```rust
105+
use std::collections::HashSet;
106+
107+
impl Solution {
108+
pub fn can_permute_palindrome(s: String) -> bool {
109+
let mut set = HashSet::new();
110+
for c in s.chars() {
111+
if set.contains(&c) {
112+
set.remove(&c);
113+
} else {
114+
set.insert(c);
115+
}
116+
}
117+
set.len() <= 1
118+
}
119+
}
120+
```
121+
86122
### **...**
87123

88124
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
pub fn can_permute_palindrome(s: String) -> bool {
5+
let mut set = HashSet::new();
6+
for c in s.chars() {
7+
if set.contains(&c) {
8+
set.remove(&c);
9+
} else {
10+
set.insert(c);
11+
}
12+
}
13+
set.len() <= 1
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function canPermutePalindrome(s: string): boolean {
2+
const set = new Set<string>();
3+
for (const c of s) {
4+
if (set.has(c)) {
5+
set.delete(c);
6+
} else {
7+
set.add(c);
8+
}
9+
}
10+
return set.size <= 1;
11+
}

0 commit comments

Comments
 (0)