public class Solution { public string LongestWord(string[] words) { var maxlist = new List<string>(); var dic = new Dictionary<string, string>(); Queue<string> Q = new Queue<string>(); var maxstr = ""; foreach (var word in words) { var temp = word; if (!dic.ContainsKey(temp)) { dic.Add(temp, temp.Substring(0, temp.Length - 1)); } } var list = dic.OrderByDescending(x => x.Key.Length).ToList(); foreach (var l in list) { var cur = l.Key; if (cur.Length < maxstr.Length) { continue; } Q.Enqueue(cur); var len = cur.Length; while (Q.Any()) { len--; var temp = Q.Dequeue(); var subtemp = temp.Substring(0, temp.Length - 1); if (dic.ContainsKey(subtemp)) { Q.Enqueue(subtemp); } } if (len == 0) { maxlist.Add(cur); maxstr = cur; } } var result = maxlist.OrderBy(x => x).ToList().FirstOrDefault(); return result; } }