Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find Case Combinations of a String
/* #21 Find Case Combinations of a String Find all the combinations of a string in lowercase and uppercase. For example, string "ab" >>> "ab", "Ab", "aB", "AB". So, you will have 2^n (n = number of chars in the string) output strings. The goal is for you to test each of these strings and see if it matchs a hidden string. */ // 1 -> 2 // 2 -> 2*2, ... n=> 2^n (0000 - 1111) // #include <vector> #include <iostream> using namespace std; char lower(char x){ if(x>='A' && x<='Z') return x-'A'+'a'; else return x; } char upper(char x) { if(x>='a' && x<='z') return x-'a'+'A'; return x; } void flip(string& s, int i){ if(s[i]>='A' && s[i]<='Z') s[i] = s[i]+'a'-'A'; else s[i] = s[i]+'A'-'a'; } vector<string> getCombination(string s){ vector<string> res; //all 2^n combinations int n = s.size(); for(int i=0; i<(1<<n); i++){ int x = i; string t = s; for(int j=0; j<n; j++){ int y = x>>j; y &= 1; // the last bit if(y&1) flip(t,j); } res.push_back(t); } return res; } int main(){ string x = "ABC";// vector<string> res = getCombination(x); for(auto x: res) cout << x << endl; return 0; }
run
|
edit
|
history
|
help
0
Roots of a Quadratic Equation
C++ - Chained Methods
codechef
FAK MEN
a
DBeach Resort 4R8J-8P (State of Rio Grande.do Norte Brazil)
test
std_minmax_unexpected_behaviour.cpp
Weighted Index
ADVENTURE CODE CSCI40