Problem
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).
Reference:
https://leetcode.com/problems...
class Solution { public List> wordSquares(String[] words) { List
> res = new ArrayList<>(); if (words == null || words.length == 0) return res; int len = words[0].length(); List
temp = new ArrayList<>(); Trie trie = new Trie(words); for (String word: words) { temp.add(word); dfs(trie, len, temp, res); temp.remove(temp.size()-1); } return res; } private void dfs(Trie trie, int len, List temp, List > res) { int size = temp.size(); if (size == len) { res.add(new ArrayList<>(temp)); return; } StringBuilder sb = new StringBuilder(); for (String str: temp) { sb.append(str.charAt(size)); } String prefix = sb.toString(); List
words = trie.getWords(prefix); for (String word: words) { temp.add(word); dfs(trie, len, temp, res); temp.remove(temp.size()-1); } } } class Trie { TrieNode root; public Trie(String[] strs) { root = new TrieNode(); for (String str: strs) { TrieNode cur = root; for (char ch: str.toCharArray()) { int index = ch-"a"; if (cur.children[index] == null) cur.children[index] = new TrieNode(); cur.children[index].words.add(str); cur = cur.children[index]; } } } public List getWords(String prefix) { List res = new ArrayList<>(); TrieNode cur = root; for (char ch: prefix.toCharArray()) { int index = ch-"a"; if (cur.children[index] == null) return res; cur = cur.children[index]; } res.addAll(cur.words); return res; } } class TrieNode { TrieNode[] children; List words; public TrieNode() { children = new TrieNode[26]; words = new ArrayList<>(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/72165.html
摘要:我們要滿足都可以作為開頭的部分。按照代碼的邏輯,走一遍填寫好每一步被更新的樣子。開始結(jié)束同一層從左向右走的時候會不斷增長,直到最后形成以個單詞相應(yīng)位置長度加一,更新重新進(jìn)行下一次的搜索。 題目描述請見leetcode 425 w a l l a r e a l e a d l a d y 在給定的詞典里,找到一些單詞,組成一個square, 第i行和第i列一樣。(0,0) 這個點(diǎn)找到滿...
摘要:題目鏈接暴力遍歷,一個一個檢查看符不符合要求。首先這種需要求出所有結(jié)果的題,一般都是用的。因?yàn)轭}目已經(jīng)說了的長度范圍是到,最多考慮五個單詞即可。首先是肯定都需要的,兩種或者。如果題目要求返回所有以特定的開頭的單詞,那么可以用。 Valid Word Square 題目鏈接:https://leetcode.com/problems... 暴力遍歷,一個一個檢查看符不符合要求。 ...
摘要:題目鏈接題目分析本題比較簡單。對給定數(shù)組的每一個數(shù)字的平方。并對結(jié)果進(jìn)行排序。思路遍歷每一個元素,相乘自身。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 977. Squares of a Sorted Array 題目鏈接 977. Squares of a Sorted Array 題目分析 本題比較簡單。對給定數(shù)組的每一個數(shù)字的平方。并對結(jié)果進(jìn)行排序。 思路 遍歷每一個元素,...
摘要:動態(tài)規(guī)劃復(fù)雜度時間空間思路如果一個數(shù)可以表示為一個任意數(shù)加上一個平方數(shù),也就是,那么能組成這個數(shù)最少的平方數(shù)個數(shù),就是能組成最少的平方數(shù)個數(shù)加上因?yàn)橐呀?jīng)是平方數(shù)了。 Perfect Squares Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4...
Problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12Output: 3 Explanation: 12 = 4 + 4 + 4.Exampl...
閱讀 3174·2021-09-10 10:51
閱讀 3355·2021-08-31 09:38
閱讀 1649·2019-08-30 15:54
閱讀 3137·2019-08-29 17:22
閱讀 3219·2019-08-26 13:53
閱讀 1967·2019-08-26 11:59
閱讀 3289·2019-08-26 11:37
閱讀 3317·2019-08-26 10:47