Created
March 10, 2020 02:12
-
-
Save kentkost/336b46c996d26a41f223128f10a19e74 to your computer and use it in GitHub Desktop.
Swap strings to get all the combinations of a string
This file contains hidden or 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
static void SwapWords(List<string> sentence, int i, List<List<string>> results) | |
{ | |
List<List<string>> combo = new List<List<string>>(); | |
if (i < sentence.Count && map.ContainsKey(sentence[i])) { | |
foreach (string s in map[sentence[i]]) { | |
List<string> temp = new List<string>(sentence); | |
temp[i] = s; | |
combo.Add(temp); | |
} | |
foreach (List<string> ls in combo) { | |
SwapWords(new List<string>(ls), i + 1, results); | |
} | |
} | |
else { | |
//Console.WriteLine(sentence.Aggregate((s1, s2) => s1 + " " + s2)); | |
//results.Add(sentence.Aggregate((s1, s2) => s1 + " " + s2)); | |
results.Add(sentence); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment