Last updated
Was this helpful?
Last updated
Was this helpful?
Given a set of words(without duplicates), find allyou can build from them.
A sequence of words forms a valid word square if thekthrow 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 alphabeta-z
.
Example 1:
Example 2:
这题求所以方案数,用深搜。但如果不剪枝的话会TLE的。这题可以用hashmap或者trie来装前缀。用前缀来剪。因为如果你加了新单词之后,没有这个前缀的词语的话,证明做不下去,我们就可以return了。例如填完第二行,发现没有le开头的词,我们就可以退出了,因为继续不下去,退出行一层换一个词再试