Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anonymous/b2e8fe824862ce9e81b8 to your computer and use it in GitHub Desktop.
Save anonymous/b2e8fe824862ce9e81b8 to your computer and use it in GitHub Desktop.
Generate all binary strings of length n with k bits set
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