forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
65 lines (51 loc) · 1.35 KB
/
main2.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
62
63
64
65
/// Source : https://leetcode.com/problems/letter-case-permutation/description/
/// Author : liuyubobobo
/// Time : 2018-03-09
#include <iostream>
#include <vector>
#include <cassert>
#include <ctype.h>
using namespace std;
/// Binary Code
///
/// Time Complexity: O(2^len(S))
/// Space Complexity: O(len(S))
class Solution {
public:
vector<string> letterCasePermutation(string S) {
int n = 0;
for(int i = 0 ; i < S.size() ; i ++)
if(isalpha(S[i])){
S[i] = tolower(S[i]);
n ++;
}
vector<string> res;
for(int i = 0 ; i < (1<<n) ; i ++){
int k = 0;
for(int j = 0 ; j < S.size() ; j ++)
if(isalpha(S[j])){
if(i & (1<<k))
S[j] = toupper(S[j]);
else
S[j] = tolower(S[j]);
k ++;
}
res.push_back(S);
}
return res;
}
};
void print_vec(const vector<string>& vec){
for(string s: vec)
cout << s << " ";
cout << endl;
}
int main() {
print_vec(Solution().letterCasePermutation("a1b2"));
cout << endl;
print_vec(Solution().letterCasePermutation("3z4"));
cout << endl;
print_vec(Solution().letterCasePermutation("12345"));
cout << endl;
return 0;
}