using System.Collections.Generic;
using System.Threading.Tasks;
public List<string> setOfWords;
public string errMsg = "Selected words were mismatched. Please try it again ";
public List<List<string>> strList = new List<List<string>>();
public void startProcess()
Stack<string> stack = new Stack<string>();
public void coreProcess(Stack<string> stack)
string strWord1 = stack.Peek();
if (returnCount(strWord1, M2) == 0)
strList.Add(new List<string>(stack));
int n = setOfWords.Count;
for (int i = 0; i < n; i++)
string strWord2 = setOfWords[i];
if (stack.Contains(strWord2))
if (returnCount(strWord2, strWord1) == 1)
Console.WriteLine(errMsg);
System.Environment.Exit(0);
static int returnCount(string firstWord, string secondWord)
int wordLength = firstWord.Length;
for (int x = 0; x < wordLength; x++)
if (firstWord[x] != secondWord[x])
public static void Main(string[] args)
Process process = new Process();
process.setOfWords = new List<string>{"band", "bond", "bear", "cars", "fond", "food", "foot", "hand", "head"};
Console.WriteLine("Set of words:" + " " + "(" + string.Join(",", process.setOfWords) + ")");
if (process.strList.Count == 0)
Console.WriteLine(process.errMsg);
System.Environment.Exit(0);
foreach (List<string> list in process.strList)
for (int y = list.Count - 1; y >= 0; y--)
Console.WriteLine(list[y]);