Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find Case Combinations of a String
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
/* #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; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.42 sec, absolute running time: 0.07 sec, cpu time: 0.01 sec, memory peak: 3 Mb, absolute service time: 0,5 sec
edit mode
|
history
|
discussion
ABC aBC AbC abC ABc aBc Abc abc