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.
|
|
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:
|
|
Example 2:
|
|
先构建一个字典树再用回溯法搜索。Trie我之前还没有写过,只知道概念,其结构是有一个26个nullptr的children指针的vector,有子树时那条边的字母对应的children元素从nullptr改为子树地址。另外还有index数组保存该节点子树中所有字符串在原words中对应的index。
|
|