3799. Word Squares II Medium

@problem@discussion
#Array#String#Backtracking#Sorting#Enumeration



1/**
2 * [3799] Word Squares II
3 *
4 * You are given a string array words, consisting of distinct 4-letter strings, each containing lowercase English letters.
5 * A word square consists of 4 distinct words: top, left, right and bottom, arranged as follows:
6 * 
7 * 	top forms the top row.
8 * 	bottom forms the bottom row.
9 * 	left forms the left column (top to bottom).
10 * 	right forms the right column (top to bottom).
11 * 
12 * It must satisfy:
13 * 
14 * 	top[0] == left[0], top[3] == right[0]
15 * 	bottom[0] == left[3], bottom[3] == right[3]
16 * 
17 * Return all valid distinct word squares, sorted in ascending lexicographic order by the 4-tuple (top, left, right, bottom)​​​​​​​.
18 *  
19 * <strong class="example">Example 1:
20 * <div class="example-block">
21 * Input: <span class="example-io">words = ["able","area","echo","also"]</span>
22 * Output: <span class="example-io">[["able","area","echo","also"],["area","able","also","echo"]]</span>
23 * Explanation:
24 * There are exactly two valid 4-word squares that satisfy all corner constraints:
25 * 
26 * 	"able" (top), "area" (left), "echo" (right), "also" (bottom)
27 * 	
28 * 		top[0] == left[0] == 'a'
29 * 		top[3] == right[0] == 'e'
30 * 		bottom[0] == left[3] == 'a'
31 * 		bottom[3] == right[3] == 'o'
32 * 	
33 * 	
34 * 	"area" (top), "able" (left), "also" (right), "echo" (bottom)
35 * 	
36 * 		All corner constraints are satisfied.
37 * 	
38 * 	
39 * 
40 * Thus, the answer is [["able","area","echo","also"],["area","able","also","echo"]].
41 * </div>
42 * <strong class="example">Example 2:
43 * <div class="example-block">
44 * Input: <span class="example-io">words = ["code","cafe","eden","edge"]</span>
45 * Output: <span class="example-io">[]</span>
46 * Explanation:
47 * No combination of four words satisfies all four corner constraints. Thus, the answer is empty array [].
48 * </div>
49 *  
50 * Constraints:
51 * 
52 * 	4 <= words.length <= 15
53 * 	words[i].length == 4
54 * 	words[i] consists of only lowercase English letters.
55 * 	All words[i] are distinct.
56 * 
57 */
58pub struct Solution {}
59
60// problem: https://leetcode.com/problems/word-squares-ii/
61// discuss: https://leetcode.com/problems/word-squares-ii/discuss/?currentPage=1&orderBy=most_votes&query=
62
63// submission codes start here
64
65impl Solution {
66    pub fn word_squares(words: Vec<String>) -> Vec<Vec<String>> {
67        vec![]
68    }
69}
70
71// submission codes end
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_3799() {
79    }
80}
81

Back
© 2026 bowen.ge All Rights Reserved.