Skip to content

Instantly share code, notes, and snippets.

@asunar
Last active December 16, 2015 14:09
Show Gist options
  • Save asunar/5447205 to your computer and use it in GitHub Desktop.
Save asunar/5447205 to your computer and use it in GitHub Desktop.
Given an array of letters, find all combinations that contain all the letters in the array
public static List<string> GetSubCombinations(List<string> letters)
{
if(letters.Count() == 1) return letters;
var combinations = new List<string>();
for(int i=0; i< letters.Count; i++)
{
var letterToInterleave = letters[i];
var remainder = letters.Where(x => x != letterToInterleave).ToList();
if (remainder.Count() == 1)
{
combinations.Add(letterToInterleave + remainder[0]);
}
else
{
var subPerms = GetSubCombinations(remainder);
foreach (var s in subPerms)
{
combinations.Add(letterToInterleave + s);
}
}
}
return combinations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment