public List <string> FindWords(char[][] board, string[] words)
    {
        //构建字典树
        WordTrie myTrie = new WordTrie();
        TrieNode root   = myTrie.root;

        foreach (string word in words)
        {
            myTrie.Insert(word);
        }

        //使用set防止重复
        HashSet <string> result = new HashSet <string>();
        int m = board.Length;
        int n = board[0].Length;

        bool[,] visited = new bool[m, n];

        //遍历整个二维数组
        for (int i = 0; i < board.Length; i++)
        {
            for (int j = 0; j < board[0].Length; j++)
            {
                Find(board, visited, i, j, m, n, result, root);
            }
        }

        return(new List <String>(result));
    }
Example #2
0
        public void TrieLowerTest()
        {
            var trie = new WordTrie();

            trie.Insert("Ab");

            Assert.True(trie.Search("ab"));
        }
Example #3
0
        public void SearchTest()
        {
            var trie = new WordTrie();

            trie.Insert("ab");
            trie.Insert("abc");
            trie.Insert("abd");
            trie.Insert("abcd");

            Assert.True(trie.Search("ab"));
            Assert.True(trie.Search("abc"));
            Assert.True(trie.Search("abd"));
            Assert.True(trie.Search("abcd"));

            Assert.False(trie.Search("z"));
            Assert.False(trie.Search("zb"));
            Assert.False(trie.Search("bc"));
            Assert.False(trie.Search("acb"));

            Assert.False(trie.Search("?"));
        }
Example #4
0
        public void ThrowsWhenUsingReservedCharacters()
        {
            Assert.Throws <System.Exception>(() => {
                var trie = new WordTrie();
                trie.Insert(trie.StartChar.ToString());
            });

            Assert.Throws <System.Exception>(() => {
                var trie = new WordTrie();
                trie.Insert(trie.EndChar.ToString());
            });
        }
Example #5
0
        public void TrieDeleteTest()
        {
            var trie = new WordTrie();

            trie.Insert("ab");
            trie.Insert("abc");
            trie.Insert("abd");

            trie.Delete("ab");
            Assert.False(trie.Search("ab"));
            Assert.True(trie.Search("abc"));
            Assert.True(trie.Search("abd"));

            trie.Delete("abc");
            Assert.False(trie.Search("ab"));
            Assert.False(trie.Search("abc"));
            Assert.True(trie.Search("abd"));

            trie.Delete("abd");
            Assert.False(trie.Search("ab"));
            Assert.False(trie.Search("abc"));
            Assert.False(trie.Search("abd"));
        }
Example #6
0
        public void TrieInsertTest()
        {
            var trie = new WordTrie();

            trie.Insert("ab");

            Assert.Equal(trie.StartChar, trie.Root.Character);

            var aNode    = new Node('a', trie.EndChar);
            var bNode    = new Node('b', trie.EndChar);
            var lastNode = new Node(trie.EndChar, trie.EndChar);

            Assert.True(trie.Root.Childs.TryGetValue(aNode, out var a));
            Assert.Equal('a', a.Character);

            Assert.True(a.Childs.TryGetValue(bNode, out var b));
            Assert.Equal('b', b.Character);

            Assert.True(b.Childs.TryGetValue(lastNode, out var last));
            Assert.Equal('=', last.Character);
        }