-
Notifications
You must be signed in to change notification settings - Fork 0
/
LetterCasePermutation*
44 lines (36 loc) · 1.02 KB
/
LetterCasePermutation*
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
/************************************/
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.
在真实的面试中遇到过这道题?
/**************************************/
class Solution {
public:
vector<string> letterCasePermutation(string S) {
vector<string> res;
backTracking(0, res, S);
return res;
}
void backTracking(int start, vector<string>& res, string s) {
if (start == s.size()) {
res.push_back(s);
return;
}
if (s[start] >= '0'&&s[start] <= '9') {
backTracking(start + 1, res, s);
return;
}
s[start] = tolower(s[start]);
backTracking(start + 1, res, s);
s[start] = toupper(s[start]);
backTracking(start + 1, res, s);
}
};