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:
There are at least 1 and at most 1000 words.
All words will have the exact same length.
Word length is at least 1 and at most 5.
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).

Backtracking + Trie:    referred to https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16

A better approach is to check the validity of the word square while we build it.
Example: ["area","lead","wall","lady","ball"]
We know that the sequence contains 4 words because the length of each word is 4.
Every word can be the first word of the sequence, let's take "wall" for example.
Which word could be the second word? Must be a word start with "a" (therefore "area"), because it has to match the second letter of word "wall".
Which word could be the third word? Must be a word start with "le" (therefore "lead"), because it has to match the third letter of word "wall" and the third letter of word "area".
What about the last word? Must be a word start with "lad" (therefore "lady"). For the same reason above.

The picture below shows how the prefix are matched while building the sequence.

In order for this to work, we need to fast retrieve all the words with a given prefix. There could be 2 ways doing this:

  1. Using a hashtable, key is prefix, value is a list of words with that prefix.
  2. Trie, we store a list of words with the prefix on each trie node.

The implemented below uses Trie.

 public class Solution {
public class TrieNode {
TrieNode[] sons;
List<String> startWith;
public TrieNode() {
this.sons = new TrieNode[26];
this.startWith = new ArrayList();
}
} public class Trie {
TrieNode root;
public Trie() {
this.root = new TrieNode();
} public void insert(String word) {
char[] arr = word.toCharArray();
TrieNode node = root;
for (char c : arr) {
if (node.sons[(int)(c-'a')] == null) {
node.sons[(int)(c-'a')] = new TrieNode();
}
node.sons[(int)(c-'a')].startWith.add(word);
node = node.sons[(int)(c-'a')];
}
} public List<String> findByPrefix(String prefix) {
char[] pre = prefix.toCharArray();
TrieNode node = root;
for (char c : pre) {
if (node.sons[(int)(c-'a')] == null) {
return new ArrayList<String>();
}
node = node.sons[(int)(c-'a')];
}
return node.startWith;
}
} public List<List<String>> wordSquares(String[] words) {
List<List<String>> res = new ArrayList<>();
if (words==null || words.length==0) return res;
int len = words[0].length(); //build trie
Trie trie = new Trie();
for (String word : words) {
trie.insert(word);
} List<String> path = new ArrayList<String>();
for (String word : words) {
path.add(word);
helper(res, path, trie, len);
path.remove(path.size()-1);
}
return res;
} public void helper(List<List<String>> res, List<String> path, Trie trie, int len) {
if (path.size() == len) {
res.add(new ArrayList<String>(path));
return;
}
int pos = path.size();
StringBuilder prefix = new StringBuilder();
for (String str : path) {
prefix.append(str.charAt(pos));
}
List<String> nextPossible = trie.findByPrefix(prefix.toString());
for (String str : nextPossible) {
path.add(str);
helper(res, path, trie, len);
path.remove(path.size()-1);
}
}
}

Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)的更多相关文章

  1. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  2. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  3. LeetCode Monotone Stack Summary 单调栈小结

    话说博主在写Max Chunks To Make Sorted II这篇帖子的解法四时,写到使用单调栈Monotone Stack的解法时,突然脑中触电一般,想起了之前曾经在此贴LeetCode Al ...

  4. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  5. [Lintcode]Word Squares(DFS|字符串)

    题意 略 分析 0.如果直接暴力1000^5会TLE,因此考虑剪枝 1.如果当前需要插入第i个单词,其剪枝如下 1.1 其前缀(0~i-1)已经知道,必定在前缀对应的集合中找 – 第一个词填了ball ...

  6. Word Squares

    Description Given a set of words without duplicates, find all word squares you can build from them. ...

  7. LC 425. Word Squares 【lock,hard】

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  8. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  9. Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

随机推荐

  1. zorka源码解读之Beanshell与zorka的交互实现

    一.beanshell基础知识从应用程序中调用BeanShell创建一个BeanShell的解释器(interpreter)用eval()和source()命令可以对一个字符串求值和运行一个脚本文件使 ...

  2. 【BO】安装BO服务器时,oracle服务端安装ora-12514和12541的问题

    今天在安装BO服务器,oracle数据库时,出现了这样一个问题,描述如下: 首先安装oracle10g Server 32位版.安装ORCL数据库之后,使用10gServer下的NET MANAGER ...

  3. XVI Open Cup named after E.V. Pankratiev. GP of Eurasia

    A. Nanoassembly 首先用叉积判断是否在指定向量右侧,然后解出法线与给定直线的交点,再关于交点对称即可. #include<bits/stdc++.h> using names ...

  4. BZOJ4078 : [Wf2014]Metal Processing Plant

    设$D(A)\leq D(B)$,从小到大枚举$D(A)$,双指针从大到小枚举$D(B)$. 那么对于权值不超过$D(A)$的边,可以忽略. 对于权值介于$(D(A),D(B)]$之间的边,需要满足那 ...

  5. JavaScript使用接口

    在经典的Java面向对象语言中,可以用关键字interface来定义接口,用implement来实现接口,而JavaScript虽然也是面向对象语言,但是它并没有内置这些,不过由于JavaScript ...

  6. PRML读书后记(一): 拟合学习

    高斯分布·拟合 1.1 优美的高斯分布 中心极限定理[P79]证明均匀分布和二项分布在数据量 $N\rightarrow \infty$ 时,都会演化近似为高斯分布. 作为最晚发现的概率分布,可以假设 ...

  7. 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)

    1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...

  8. About_PHP_验证码的生成

    验证码就是一张图片,用到几个关键字: <?php session_start(); $arr = array( 'a','b','c','d','e','f','g','h','i','j',' ...

  9. Maya Plugin 编译Maya插件

    Maya自身的功能就已经非常强大了,但是更棒的是它的扩展性非常强,提供API让用户自己来编写插件Plugin.Maya的插件主要是两种,一种是用C++编写的,后缀为".mll",另 ...

  10. java使用poi包将数据写入Excel表格

    1.Excel相关操作代码 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...