Skip to content

Commit 04c6efe

Browse files
committed
feat: add solutions to lc problem: No.0784
No.0784.Letter Case Permutation
1 parent 1777721 commit 04c6efe

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

solution/0700-0799/0784.Letter Case Permutation/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,54 @@ func letterCasePermutation(s string) []string {
151151
}
152152
```
153153

154+
### **TypeScript**
155+
156+
```ts
157+
function letterCasePermutation(s: string): string[] {
158+
const n = s.length;
159+
const cs = [...s];
160+
const res = [];
161+
const dfs = (i: number) => {
162+
if (i === n) {
163+
res.push(cs.join(''));
164+
return;
165+
}
166+
dfs(i + 1);
167+
if (cs[i] >= 'A') {
168+
cs[i] = String.fromCharCode(cs[i].charCodeAt(0) ^ 32);
169+
dfs(i + 1);
170+
}
171+
};
172+
dfs(0);
173+
return res;
174+
}
175+
```
176+
177+
### **Rust**
178+
179+
```rust
180+
impl Solution {
181+
fn dfs(i: usize, cs: &mut Vec<char>, res: &mut Vec<String>) {
182+
if i == cs.len() {
183+
res.push(cs.iter().collect());
184+
return;
185+
}
186+
Self::dfs(i + 1, cs, res);
187+
if cs[i] >= 'A' {
188+
cs[i] = char::from((cs[i] as u8) ^ 32);
189+
Self::dfs(i + 1, cs, res);
190+
}
191+
}
192+
193+
pub fn letter_case_permutation(s: String) -> Vec<String> {
194+
let mut res = Vec::new();
195+
let mut cs = s.chars().collect::<Vec<char>>();
196+
Self::dfs(0, &mut cs, &mut res);
197+
res
198+
}
199+
}
200+
```
201+
154202
### **...**
155203

156204
```

solution/0700-0799/0784.Letter Case Permutation/README_EN.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,54 @@ func letterCasePermutation(s string) []string {
133133
}
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
function letterCasePermutation(s: string): string[] {
140+
const n = s.length;
141+
const cs = [...s];
142+
const res = [];
143+
const dfs = (i: number) => {
144+
if (i === n) {
145+
res.push(cs.join(''));
146+
return;
147+
}
148+
dfs(i + 1);
149+
if (cs[i] >= 'A') {
150+
cs[i] = String.fromCharCode(cs[i].charCodeAt(0) ^ 32);
151+
dfs(i + 1);
152+
}
153+
};
154+
dfs(0);
155+
return res;
156+
}
157+
```
158+
159+
### **Rust**
160+
161+
```rust
162+
impl Solution {
163+
fn dfs(i: usize, cs: &mut Vec<char>, res: &mut Vec<String>) {
164+
if i == cs.len() {
165+
res.push(cs.iter().collect());
166+
return;
167+
}
168+
Self::dfs(i + 1, cs, res);
169+
if cs[i] >= 'A' {
170+
cs[i] = char::from((cs[i] as u8) ^ 32);
171+
Self::dfs(i + 1, cs, res);
172+
}
173+
}
174+
175+
pub fn letter_case_permutation(s: String) -> Vec<String> {
176+
let mut res = Vec::new();
177+
let mut cs = s.chars().collect::<Vec<char>>();
178+
Self::dfs(0, &mut cs, &mut res);
179+
res
180+
}
181+
}
182+
```
183+
136184
### **...**
137185

138186
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
fn dfs(i: usize, cs: &mut Vec<char>, res: &mut Vec<String>) {
3+
if i == cs.len() {
4+
res.push(cs.iter().collect());
5+
return;
6+
}
7+
Self::dfs(i + 1, cs, res);
8+
if cs[i] >= 'A' {
9+
cs[i] = char::from((cs[i] as u8) ^ 32);
10+
Self::dfs(i + 1, cs, res);
11+
}
12+
}
13+
14+
pub fn letter_case_permutation(s: String) -> Vec<String> {
15+
let mut res = Vec::new();
16+
let mut cs = s.chars().collect::<Vec<char>>();
17+
Self::dfs(0, &mut cs, &mut res);
18+
res
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function letterCasePermutation(s: string): string[] {
2+
const n = s.length;
3+
const cs = [...s];
4+
const res = [];
5+
const dfs = (i: number) => {
6+
if (i === n) {
7+
res.push(cs.join(''));
8+
return;
9+
}
10+
dfs(i + 1);
11+
if (cs[i] >= 'A') {
12+
cs[i] = String.fromCharCode(cs[i].charCodeAt(0) ^ 32);
13+
dfs(i + 1);
14+
}
15+
};
16+
dfs(0);
17+
return res;
18+
}

0 commit comments

Comments
 (0)