using System; using System.Collections.Generic; using System.Linq; public class Test { public static void Main() { List validatedStrings = new List(); List subjectStrings = new List() { "con", "cot", "eon", "net", "not", "one", "ten", "toe", "ton", "cent", "cone", "conn", "cote", "neon", "none", "note", "once", "tone", "cento", "conte", "nonce", "nonet", "oncet", "tenon", "tonne", "nocent","concent", "connect" }; //got a more longer wordlist string startswithString = "co"; string endswithString = "et"; foreach(var z in subjectStrings) { bool valid = false; foreach(var a in getCombinations(startswithString)) { foreach(var b in getCombinations(endswithString)) { if(z.StartsWith(a) && !z.EndsWith(b)) { valid = true; break; } } if(valid) { break; } } if(valid) { validatedStrings.Add(z); } } foreach(var a in validatedStrings) { Console.WriteLine(a); } Console.WriteLine("\nDone"); } static List getCombinations(string s) { //Code that calculates combinations return Permutations.Permutate(s); } } public class Permutations { private static List> allCombinations; private static void CalculateCombinations(string word, List temp) { if (temp.Count == word.Length) { List clone = temp.ToList(); if (clone.Distinct().Count() == clone.Count) { allCombinations.Add(clone); } return; } for (int i = 0; i < word.Length; i++) { temp.Add(word[i].ToString()); CalculateCombinations(word, temp); temp.RemoveAt(temp.Count - 1); } } public static List Permutate(string str) { allCombinations = new List>(); CalculateCombinations(str, new List()); List combinations = new List(); foreach(var a in allCombinations) { string c = ""; foreach(var b in a) { c+=b; } combinations.Add(c); } return combinations; } }