425. Word Squares
Given a set of words (without duplicates), find all word squares you can build from them. A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns). For example, the word sequence ["ball","area","lead","lady"] forms a word square because each word reads the same both horizontally and vertically. b a l l a r e a l e a d l a d y Note: 1. There are at least 1 and at most 1000 words. 2. All words will have the exact same length. 3. Word length is at least 1 and at most 5. 4. Each word contains only lowercase English alphabet a-z. Example 1: Input: ["area","lead","wall","lady","ball"] Output: [ [ "wall", "area", "lead", "lady" ], [ "ball", "area", "lead", "lady" ] ] Explanation: The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters). Example 2: Input: ["abat","baba","atan","atal"] Output: [ [ "baba", "abat", "baba", "atan" ], [ "baba", "abat", "baba", "atal" ] ] Explanation: The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters). 小班课件 https://www.youtube.com/watch?v=TdX3EgW4_eM https://leetcode.com/problems/word-squares/discuss/91333/Explained.-My-Java-solution-using-Trie-126ms-1616 这个trie , trienode class 的结构 做这个trie 的结构的时候, 注意 List<String> startWith 把 所有以这个prefix 的 word 都加了进来 我觉得我的 ood 还不扎实。。 class Solution { class TrieNode{ List<String> startWith; TrieNode[] children; TrieNode(){ startWith = new ArrayList<>(); children = new TrieNode[26]; } } class Trie{ TrieNode root; Trie(String[] words){ root = new TrieNode(); for(String w : words){ TrieNode cur = root; for(char ch : w.toCharArray()){ int index = ch - 'a'; if(cur.children[index] == null){ cur.children[index] = new TrieNode(); } cur.children[index].startWith.add(w); cur = cur.children[index]; } } } List<String> getCandidates(String prefix){ List<String> ans = new ArrayList<>(); TrieNode cur = root; for(char ch : prefix.toCharArray()){ int index = ch - 'a'; if(cur.children[index] == null){ return ans; } cur = cur.children[index]; } ans.addAll(cur.startWith); return ans; } } public List<List<String>> wordSquares(String[] words) { List<List<String>> ans = new ArrayList<>(); if(words == null || words.length == 0) return ans; int len = words[0].length(); Trie trie = new Trie(words); List<String> solution = new ArrayList<>(); for(String w : words){ solution.add(w); search(solution, ans, trie, len); solution.remove(solution.size() - 1); } return ans; } private void search(List<String> solution, List<List<String>> ans, Trie trie, int len){ // base case if(solution.size() == len){ ans.add(new ArrayList<>(solution)); return; } // dfs int index = solution.size(); StringBuilder prefix = new StringBuilder(); for(String word : solution){ prefix.append(word.charAt(index)); } List<String> candidates = trie.getCandidates(prefix.toString()); for(String candidate : candidates){ solution.add(candidate); search(solution, ans, trie, len); solution.remove(solution.size() - 1); } } }
posted on 2018-11-06 09:29 猪猪🐷 阅读(119) 评论(0) 收藏 举报
浙公网安备 33010602011771号