예제 #1
0
        public static string LongestWord(string[] words)
        {
            Dictionary <string, bool> results = new Dictionary <string, bool>();

            foreach (string word in words)
            {
                results[word] = true;
            }

            foreach (string word in words.OrderByDescending(s => s.Length))
            {
                if (LongestWordClass.CanBuild(word, true, results))
                {
                    return(word);
                }
            }

            return(string.Empty);
        }
예제 #2
0
        private static bool CanBuild(string s, bool isWord, Dictionary <string, bool> results)
        {
            bool result = false;

            if (!isWord && results.TryGetValue(s, out result))
            {
                return(result);
            }

            result = false;

            for (int i = 1; i < s.Length - 1; i++)
            {
                if (LongestWordClass.CanBuild(s.Substring(0, i), false, results) && LongestWordClass.CanBuild(s.Substring(i), false, results))
                {
                    result = true;
                    break;
                }
            }

            results[s] = result;
            return(result);
        }
예제 #3
0
 private static void LongestWordTest(string[] words, string expected)
 {
     Assert.AreEqual(expected, LongestWordClass.LongestWord(words));
 }