Created
February 6, 2015 15:36
-
-
Save anonymous/b2e8fe824862ce9e81b8 to your computer and use it in GitHub Desktop.
Generate all binary strings of length n with k bits set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<string> answer; | |
void getStrings( string s, int digitsLeft ) | |
{ | |
if( digitsLeft == 0 ) // the length of string is n | |
answer.push_back( s ); | |
else | |
{ | |
getStrings( s + "0", digitsLeft - 1 ); | |
getStrings( s + "1", digitsLeft - 1 ); | |
} | |
} | |
getStrings( "", n ); // initial call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment