Java solution with Trie


  • 0
    Y

    The implementation of Trie data structure could be found at:
    http://www.programcreek.com/2014/05/leetcode-implement-trie-prefix-tree-java/

    With Solution 2 provided:

    class TrieNode {
        TrieNode[] arr;
        boolean isEnd;
        // Initialize your data structure here.
        public TrieNode() {
            this.arr = new TrieNode[26];
        }
     
    }
     
    public class Trie {
        private TrieNode root;
     
        public Trie() {
            root = new TrieNode();
        }
     
        // Inserts a word into the trie.
        public void insert(String word) {
            TrieNode p = root;
            for(int i=0; i<word.length(); i++){
                char c = word.charAt(i);
                int index = c-'a';
                if(p.arr[index]==null){
                    TrieNode temp = new TrieNode();
                    p.arr[index]=temp;
                    p = temp;
                }else{
                    p=p.arr[index];
                }
            }
            p.isEnd=true;
        }
     
        // Returns if the word is in the trie.
        public boolean search(String word) {
            TrieNode p = searchNode(word);
            if(p==null){
                return false;
            }else{
                if(p.isEnd)
                    return true;
            }
     
            return false;
        }
     
        // Returns if there is any word in the trie
        // that starts with the given prefix.
        public boolean startsWith(String prefix) {
            TrieNode p = searchNode(prefix);
            if(p==null){
                return false;
            }else{
                return true;
            }
        }
     
        public TrieNode searchNode(String s){
            TrieNode p = root;
            for(int i=0; i<s.length(); i++){
                char c= s.charAt(i);
                int index = c-'a';
                if(p.arr[index]!=null){
                    p = p.arr[index];
                }else{
                    return null;
                }
            }
     
            if(p==root)
                return null;
     
            return p;
        }
    }
    

    Please see below for the solution:
    Within helper(), split the word into two parts. Then if the first part is a word, call helper() recursively for the second part.

    	private boolean helper(Trie root, int count, String word) {
    		if (root.search(word) && count > 0)
    			return true;
    		for (int i = 1; i < word.length(); i++) {
    			if (root.search(word.substring(0, i)))
    				if (helper(root, count + 1, word.substring(i)))
    					return true;
    		}
    		return false;
    	}
        public List<String> findAllConcatenatedWordsInADict(String[] words) {
    	List<String> res = new LinkedList<>();
            Trie root = new Trie();
    	for (int i = 0; i < words.length; i++)
    		root.insert(words[i]);
    	for (int i = 0; i < words.length; i++)
    		if (helper(root, 0, words[i]))
    			res.add(words[i]);
    	return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.