public int F(string prefix, string suffix) { HashSet <int> prefixWords = prefixTree.GetWordIndicesWithPrefix(prefix); char[] charArray = suffix.ToCharArray(); Array.Reverse(charArray); suffix = new string(charArray); HashSet <int> suffixWords = suffixTree.GetWordIndicesWithPrefix(suffix); // Find the union of words in both and take the largest. int largest = -1; foreach (int s in suffixWords) { if (prefixWords.Contains(s)) { largest = Math.Max(s, largest); } } return(largest); }