-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy path16. Letter Case Permutation.cpp
61 lines (51 loc) · 1.11 KB
/
16. Letter Case Permutation.cpp
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
60
61
/*
Letter Case Permutation
=======================
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. You can return the output in any order.
Example 1:
Input: S = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]
Example 2:
Input: S = "3z4"
Output: ["3z4","3Z4"]
Example 3:
Input: S = "12345"
Output: ["12345"]
Example 4:
Input: S = "0"
Output: ["0"]
Constraints:
S will be a string with length between 1 and 12.
S will consist only of letters or digits.
*/
class Solution
{
public:
void recurse(string s, int curr, vector<string> &ans)
{
if (curr == s.size())
{
ans.push_back(s);
return;
}
if (isalpha(s[curr]))
{
string n1 = s, n2 = s;
n1[curr] = toupper(n1[curr]);
n2[curr] = tolower(n2[curr]);
recurse(n2, curr + 1, ans);
recurse(n1, curr + 1, ans);
}
else
{
recurse(s, curr + 1, ans);
}
}
vector<string> letterCasePermutation(string S)
{
vector<string> ans;
recurse(S, 0, ans);
return ans;
}
};