Created
January 13, 2018 06:53
-
-
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.
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
/* | |
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