예제 #1
0
        private static void TestMyDictionary()
        {
            MyWordDictionary d       = new MyWordDictionary();
            bool             added   = d.AddWord("Hello");
            bool             isValid = d.IsValidWord("Hello");

            isValid = d.IsValidWord("Hell");
        }
예제 #2
0
        private static MyWordDictionary PopulateSomeWordsInDictionary()
        {
            MyWordDictionary d = new MyWordDictionary();

            d.AddWord("be");
            d.AddWord("beg");
            d.AddWord("begun");
            d.AddWord("cod");
            d.AddWord("code");
            return(d);
        }
예제 #3
0
        //Negative Test cases
        // 1. Passing null string/empty string
        // 2. position is negative
        // 3. position reaching int.Max 2^31
        // 4. string all aaaaaaaaaa, position > 1
        // 5. All No-unique char string   abcdefg, position >=1
        // Positive test cases
        // 1. one or more unique char , position is >=1 => String.Empty or a char
        // 2. string with mixed case char x where it should skip to the next unique char
        // 3. spaces ??

        /*
         * Given a string determine if it consist of valid concatenated words.
         * "dogcatfish" --> true because it can be split int "dog","cat", and fish
         * "dogecatfish" --> false
         * */

        public static bool AreAllValidWords(string inStr, MyWordDictionary dict)
        {
            if (string.IsNullOrEmpty(inStr))
            {
                return(false);
            }

            Mark[] P = new Mark[inStr.Length];
            P[0].isWord = dict.IsValidWord(inStr, 0, 0);

            for (int i = 1; i < inStr.Length; i++)
            {
                if (dict.IsValidWord(inStr, 0, i))
                {
                    P[i].isWord = true;
                    P[i].sIdx   = 0;
                }
                else
                {
                    for (int j = 1; j <= i; j++)
                    {
                        if (P[j - 1].isWord && dict.IsValidWord(inStr, j, i))
                        {
                            P[i].isWord = true;
                            P[i].sIdx   = j;
                            break;
                        }
                    }
                }
            }

            //Now that P is populated, check that you have no gaps.
            Mark m = P[inStr.Length - 1];

            do
            {
                if (!m.isWord)
                {
                    return(false);
                }
                else
                {
                    m = P[m.sIdx - 1];
                }
            } while (m.sIdx > 0);

            return(true);
        }