Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 13, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/fb2fb3b65e0a85419c34dc74214af9dd to your computer and use it in GitHub Desktop.
Save jianminchen/fb2fb3b65e0a85419c34dc74214af9dd to your computer and use it in GitHub Desktop.
Find all possible strings - each char in the string can be lower case or upper case.
/*
abc
output includes 8 strings. 8 combinations of string.
Abc
ABc
ABC
abc
aBc
abC
aBC
ABC
Please output all possible strings, for each char in the string, there are two choices,
one is lower case, another one is upper case.
*/
public static void FindAllCases(string s, string prefix, List<string> output) // "abc", "", new List<string>()
{
// base case
if(s == null) // false
{
output.Add(prefix);
return;
}
var current = s[0]; // 'a'
var capitalChar = (char)(current - 'a' + 'A'); // 'A'
var nextString = s.substring(1, s.Length - 1); // "bc"
var nextPrefix = prefix + current.Tostring(); // "" + "a"
var nextPrefixCamel = previx + capitalChar.Tostring(); // "" + "A"
FindAllCases(nextString, nextPrefix, output); // "bc", "a", output
FindAllCases(nextString, nextPrefix, output); // "bc", "A", output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment