784. Letter Case Permutation

problem

solution

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
class Solution {
public:
vector<string> ret;
void traverse(string s, string path,int l){

if(path.size() == s.size()){
ret.push_back(path);
return;
}

for(int i=l;i<s.size() ; ++i){

// 因為只會有大小寫與數字
if(s[i] >='0' && s[i]<='9'){
path.push_back(s[i]);
traverse(s, path, i+1);
path.pop_back();

}
// if(( s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z')){
else{
// 選擇小寫
path.push_back(tolower(s[i]));
traverse(s, path, i+1);
path.pop_back();

// 選擇大寫
path.push_back(toupper(s[i]));
traverse(s, path, i+1);
path.pop_back();
}
}

}
vector<string> letterCasePermutation(string s) {

string path;
traverse(s, path, 0);
return ret;

}
};