-
Notifications
You must be signed in to change notification settings - Fork 24
/
0784-letter-case-permutation.js
59 lines (51 loc) · 1.34 KB
/
0784-letter-case-permutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 784. Letter Case Permutation
// Easy 62%
// Given a string S, we can transform every letter individually to be lowercase
// or uppercase to create another string. Return a list of all possible strings
// we could create.
// Examples:
// Input: S = "a1b2"
// Output: ["a1b2", "a1B2", "A1b2", "A1B2"]
// Input: S = "3z4"
// Output: ["3z4", "3Z4"]
// Input: S = "12345"
// Output: ["12345"]
// Note:
// S will be a string with length between 1 and 12.
// S will consist only of letters or digits.
/**
* @param {string} S
* @return {string[]}
*/
const letterCasePermutation = function(S) {
const res = [], n = S.length
S = S.split('')
function rec(i) {
while ('0' <= S[i] && S[i] <= '9') i++
if (i >= n) {
res.push(S.join(''))
return
}
S[i] = S[i].toLowerCase()
rec(i + 1)
S[i] = S[i].toUpperCase()
rec(i + 1)
}
rec(0)
return res
}
;[
'a1b2',
'3z4',
'12345',
'abc',
].forEach((S) => {
console.log(letterCasePermutation(S))
})
// Solution:
// 使用递归的方法(DFS)
// 遇到一个字符,就先将其转换成小写/大写,然后遍历下一个字符
// 循环的方法
// 遇到一个字符,将 res 中的每个字符串分为两个,两个字符串只是当前字符不同。
// JS 中改变字符串中的某个字符不是很方便,作罢。
// Submission Result: Accepted