Java Simple/Classical Trie question/solution (Beat 96%)


  • 7
    public String replaceWords(List<String> dict, String sentence) {
            String[] tokens = sentence.split(" ");
            TrieNode trie = buildTrie(dict);
            return replaceWords(tokens, trie);
        }
    
        private String replaceWords(String[] tokens, TrieNode root) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String token : tokens) {
                stringBuilder.append(getShortestReplacement(token, root));
                stringBuilder.append(" ");
            }
            return stringBuilder.substring(0, stringBuilder.length()-1);
        }
    
        private String getShortestReplacement(String token, final TrieNode root) {
            TrieNode temp = root;
            StringBuilder stringBuilder = new StringBuilder();
            for (char c : token.toCharArray()) {
                stringBuilder.append(c);
                if (temp.children[c - 'a'] != null) {
                    if (temp.children[c - 'a'].isWord) {
                        return stringBuilder.toString();
                    }
                    temp = temp.children[c - 'a'];
                } else {
                    return token;
                }
            }
            return token;
        }
    
        private TrieNode buildTrie(List<String> dict) {
            TrieNode root = new TrieNode(' ');
            for (String word : dict) {
                TrieNode temp = root;
                for (char c : word.toCharArray()) {
                    if (temp.children[c - 'a'] == null) {
                        temp.children[c - 'a'] = new TrieNode(c);
                    }
                    temp = temp.children[c - 'a'];
                }
                temp.isWord = true;
            }
            return root;
        }
    
        public class TrieNode {
            char val;
            TrieNode[] children;
            boolean isWord;
    
            public TrieNode(char val) {
                this.val = val;
                this.children = new TrieNode[26];
                this.isWord = false;
            }
        }
    

    Also viewable on Github here.


  • 0

    Great solution! And you can simply combine the first two replaceWords together...


  • 0
    F

    Same idea

    public class Solution {
        public class TrieNode{
            boolean isWord;
            TrieNode[] next;
            public TrieNode(){
                next = new TrieNode[26];
            }
        }
        
        TrieNode root = new TrieNode();
        
        public void add(String word){
            TrieNode cur = root;
            for(int i = 0;i<word.length();i++){
                int c = word.charAt(i)-'a';
                if(cur.next[c] == null){
                    cur.next[c] = new TrieNode();
                }
                cur = cur.next[c];
            }
            cur.isWord = true;
        }
        
        public String findRoot(String word){
            TrieNode cur = root;
            int i = 0;
            for(;i<word.length();i++){
                int c = word.charAt(i)-'a';
                if(cur.next[c] == null){
                    return word;
                }
                if(cur.next[c].isWord){
                    return word.substring(0,i+1);
                }
                cur = cur.next[c];
            }
            return word;
        }
        
        public String replaceWords(List<String> dict, String sentence) {
            for(int i = 0;i<dict.size();i++){
                add(dict.get(i));
            }
            String[] str = sentence.split(" ");
            for(int i = 0;i<str.length;i++){
                str[i] = findRoot(str[i]);
            }
            return String.join(" ",str);
        }
    }
    

Log in to reply
 

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