java 425. Word Squares(1st).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 425. Word Squares(1st).java相关的知识,希望对你有一定的参考价值。

class TrieNode {
    TrieNode[] children;
    boolean hasWord;
    String word;
    List<String> startsWith;
    
    public TrieNode() {
        children = new TrieNode[26];
        hasWord = false;
        startsWith = new ArrayList<String>();
    }
}

class TrieTree {
    
    TrieNode root;
    
    public TrieTree() {
        root = new TrieNode();
    }
    
    public void buildTrieTree(String[] words) {
        for (String word : words) {
            TrieNode temp = root;
            for (char c : word.toCharArray()) {
                int idx = c - 'a';
                if (temp.children[idx] == null) temp.children[idx] = new TrieNode();
                temp.startsWith.add(word);
                temp = temp.children[idx];
            }
            temp.hasWord = true;
            temp.word = word;
        }
        //return root;
    }
    
    public List<String> findByPrefix(String prefix) {
        List<String> res = new ArrayList<String>();
        TrieNode temp = root;
        for (char c : prefix.toCharArray()) {
            int idx = c - 'a';
            if (temp.children[idx] == null) return res;
            temp = temp.children[idx];
        }
        res.addAll(temp.startsWith);
        return res;
        
    }
}
public class Solution {
    
    public void helper(TrieTree tree, int len, List<String> temp, List<List<String>> res) {
        if (temp.size() == len) {
            res.add(new ArrayList<String>(temp));
            return;
        }
        int idx = temp.size();
        StringBuilder sb = new StringBuilder();
        for (String word : temp) {
            sb.append(word.charAt(idx));
        }
        List<String> prefixWords = tree.findByPrefix(sb.toString());
        for (String word : prefixWords) {
            temp.add(word);
            helper(tree, len, temp, res);
            temp.remove(temp.size() - 1);
        }
    }
    public List<List<String>> wordSquares(String[] words) {
        TrieTree tree = new TrieTree();
        tree.buildTrieTree(words);
        int len = words[0].length();
        
        List<List<String>> res = new ArrayList<List<String>>();
        List<String> temp = new ArrayList<String>();
        for (String word : words) {
            temp.add(word);
            helper(tree, len, temp, res);
            temp.remove(temp.size() - 1);
        }
        return res;
    }
}
public class Solution {
    class Node{
        Node[] nodes;
        String word;
        Node(){
            this.nodes = new Node[26];
            this.word = null;
        }
    }
    void add(Node root, String word){
        Node node = root;
        for (char c : word.toCharArray() ) {
            int idx = c-'a';
            if (node.nodes[idx] == null) node.nodes[idx] = new Node();
            node = node.nodes[idx];
        }
        node.word = word;
    }
    void helper(int row, int col, int len, Node[] rows, List<List<String>> ret) {
        if ( (col == row) && (row == len) ) { // last char
            List<String> res = new ArrayList<String>();
            for (int i=0; i<len; i++) {
                res.add(new String(rows[i].word) );
            }
            ret.add( res );
        } else { // from left to right and then go down to the next row
            if ( col < len  ) { // left to right first
                Node pre_row = rows[row];
                Node pre_col = rows[col];
                for (int i=0; i<26; i++) { // find all the possible next char
                    if ( (rows[row].nodes[i] != null) && (rows[col].nodes[i] != null) ) {
                        rows[row] = rows[row].nodes[i];
                        if (col != row) rows[col] = rows[col].nodes[i];
                        helper(row, col+1, len, rows, ret);
                        rows[row] = pre_row;
                        if (col != row) rows[col] = pre_col;
                    }
                }
            } else { // reach the end of column, go to the next row
                helper(row+1, row+1, len, rows, ret);
            }
        }
    }
    public List<List<String>> wordSquares(String[] words) {
        List<List<String>> ret = new ArrayList();
        if (words==null || words.length==0) return ret;
        Node root = new Node();
        int len = words[0].length();
        for (String word: words) add(root, word);
        Node[] rows = new Node[len];
        for (int i=0; i<len; i++) rows[i]=root;
        helper(0, 0, len, rows, ret);
        return ret;
    }
}
public class Solution {
    class TrieNode{
        String word;
        TrieNode[] children;
        public TrieNode(){
            this.children = new TrieNode[26];
        }
    }
    
    public List<List<String>> wordSquares(String[] words) {
        List<List<String>> res = new ArrayList<>();
        if(words == null || words.length == 0){
            return res;
        }
        TrieNode root = buildtree(words);
        TrieNode[] nodes = new TrieNode[words[0].length()];
        Arrays.fill(nodes, root);
        dfs(0, 0, nodes, res);
        return res;
    }
    
    public void dfs(int idx, int level, TrieNode[] nodes, List<List<String>> res) {
        if(idx == nodes.length){
            List<String> temp= new ArrayList<>();
            for(TrieNode node: nodes){
                temp.add(node.word);
            }
            res.add(temp);
            return;
        }
        if(level == nodes.length){
            dfs(idx + 1, idx + 1, nodes, res);
        } else{
            TrieNode cur = nodes[idx];
            TrieNode diag = nodes[level];
            for(int i = 0; i < 26; i++) {
                if(cur.children[i] != null && diag.children[i] != null) {
                    nodes[idx] = cur.children[i];
                    nodes[level] = diag.children[i];
                    dfs(idx, level + 1, nodes, res);  
                }
            }
            nodes[idx] = cur;
            nodes[level] = diag;
        }
    }
    
    public TrieNode buildtree(String[] words){
        TrieNode root = new TrieNode();
        for(int i = 0; i < words.length; i++) {
            char[] chars = words[i].toCharArray();
            TrieNode node = root;
            for(int j = 0; j < chars.length; j++){
                if(node.children[chars[j] - 'a'] == null) {
                   node.children[chars[j] - 'a'] = new TrieNode();
                } 
                node = node.children[chars[j] - 'a'];
            }
            node.word = words[i];
        }
        return root;
    }
}

以上是关于java 425. Word Squares(1st).java的主要内容,如果未能解决你的问题,请参考以下文章

java 425. Word Squares(1st).java

java 425. Word Squares(1st).java

java 425. Word Squares(1st).java

java 425. Word Squares(1st).java

java 425. Word Squares(1st).java

java 425. Word Squares(1st).java