namespace Interview_Other
using System.Collections.Generic;
public enum StringConversion
public static void Main(string[] args)
Dictionary<Tuple<string, string>, StringConversion> tests = new Dictionary<Tuple<string, string>, StringConversion>{
{new Tuple<string,string>("command","command"), StringConversion.None},
{new Tuple<string,string>("ommand", "xommand"), StringConversion.Insert},
{new Tuple<string,string>("comman", "commanx"), StringConversion.Insert},
{new Tuple<string,string>("xommand", "ommand"),StringConversion.Delete},
{new Tuple<string,string>("commanx", "comman"),StringConversion.Delete},
{new Tuple<string,string>("command", "comxand"),StringConversion.Swap},
{new Tuple<string,string>("command", "xommand"),StringConversion.Swap},
{new Tuple<string,string>("command", "commanx"),StringConversion.Swap},
{new Tuple<string,string>("command", "xxmmand"),StringConversion.Unknown},
{new Tuple<string,string>("command", "dommanc"),StringConversion.Unknown},
tests.ToList().ForEach(kvp => {
var result = Compare(kvp.Key.Item1, kvp.Key.Item2);
var success = result == kvp.Value ? "Success" : "Failure";
Console.WriteLine(success + ": ("+kvp.Key.Item1 +"," + kvp.Key.Item2+") => "+result+" - expected "+kvp.Value+"");
public static StringConversion Compare(string a, string b)
var conversionType = StringConversion.Unknown;
conversionType = StringConversion.None;
else if (a.Length == b.Length)
conversionType = StringConversion.Swap;
differences = CheckInsertDeleteSwap(a, b, conversionType);
else if (a.Length + 1 == b.Length)
conversionType = StringConversion.Insert;
differences = CheckInsertDeleteSwap(a, b);
else if (a.Length == b.Length + 1)
conversionType = StringConversion.Delete;
differences = CheckInsertDeleteSwap(b, a);
return StringConversion.Unknown;
static int CheckInsertDeleteSwap(string small, string big, StringConversion conversionType = 0)
for (int i = 0; i < small.Length; i++)
if (differences > 1 || small.Length <= i)
if (conversionType == StringConversion.Swap)
small = small.Remove(i, 1);