Last active
December 16, 2015 14:09
-
-
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
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
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