Skip to content

Instantly share code, notes, and snippets.

@kentkost
Created March 10, 2020 02:12
Show Gist options
  • Save kentkost/336b46c996d26a41f223128f10a19e74 to your computer and use it in GitHub Desktop.
Save kentkost/336b46c996d26a41f223128f10a19e74 to your computer and use it in GitHub Desktop.
Swap strings to get all the combinations of a string
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