Skip to content

Commit

Permalink
0784 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Mar 9, 2018
1 parent 123b84f commit 4317622
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0784-Letter-Case-Permutation/cpp-0784/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(cpp_0784)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main2.cpp)
add_executable(cpp_0784 ${SOURCE_FILES})
71 changes: 71 additions & 0 deletions 0784-Letter-Case-Permutation/cpp-0784/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/// 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;

/// Backtrack
///
/// Time Complexity: O(2^len(S))
/// Space Complexity: O(len(S))
class Solution {

public:
vector<string> letterCasePermutation(string S) {

for(int i = 0 ; i < S.size() ; i ++)
if(isupper(S[i]))
S[i] = tolower(S[i]);

vector<string> res;
solve(S, 0, res);
return res;
}

private:
void solve(string& S, int index, vector<string>& res){

if(index == S.size()){
res.push_back(S);
return;
}

if(isalpha(S[index])){
assert(islower(S[index]));
solve(S, index + 1, res);
S[index] = toupper(S[index]);
solve(S, index + 1, res);
S[index] = tolower(S[index]);
}
else{
assert(isdigit(S[index]));
solve(S, index + 1, 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;
}
65 changes: 65 additions & 0 deletions 0784-Letter-Case-Permutation/cpp-0784/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ email: [[email protected]](mailto:[email protected])
| 781 | [Rabbits in Forest](https://leetcode.com/problems/rabbits-in-forest/description/) | [solution](https://leetcode.com/problems/rabbits-in-forest/solution/) | [C++](0781-Rabbits-in-Forest/cpp-0781/) | | |
| | | | | | |
| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/) | [solution](https://leetcode.com/problems/minimum-distance-between-bst-nodes/solution/) | [C++](0783-Minimum-Distance-Between-BST-Nodes/cpp-0783/) | | |
| 784 | [Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/description/) | [solution](https://leetcode.com/problems/letter-case-permutation/solution/) | [C++](0784-Letter-Case-Permutation/cpp-0784/) | | |
| | | | | | |
| 788 | [Rotated Digits](https://leetcode.com/problems/rotated-digits/description/) | [solution](https://leetcode.com/problems/rotated-digits/solution/) | [C++](0788-Rotated-Digits/cpp-0788/) | | |
| 789 | [Escape The Ghosts](https://leetcode.com/problems/escape-the-ghosts/description/) | [solution](https://leetcode.com/problems/escape-the-ghosts/solution/) | [C++](0789-Escape-The-Ghosts/cpp-0789/) | | |
Expand Down

0 comments on commit 4317622

Please sign in to comment.