Find every word square that can be built from a list of equal-length words

Quick Overview

Given a list of unique words of equal length, return every word square they can form, where each row reads the same as the matching column, sorted row by row. It tests backtracking with prefix pruning and building a trie or prefix map for fast candidate lookup.

Find every word square that can be built from a list of equal-length words

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given a list of unique lowercase strings `words`, all of the same length `L`, return every word square that can be built from them. The same word may be used more than once in a square. A sequence of words forms a word square when, for every index `k`, the `k`th row and the `k`th column read the same string. Because every word has length `L`, a square has exactly `L` rows. For example, `["ball","area","lead","lady"]` is a word square: ```text b a l l a r e a l e a d l a d y ``` Row 0 and column 0 both read "ball", row 1 and column 1 both read "area", and the same holds for rows 2 and 3. ### Function Signature ```python def word_squares(words: list[str]) -> list[list[str]]: ``` ### Rules - Each square is returned as a list of `L` words in row order, row 0 first. - Return the squares sorted in ascending lexicographic order, comparing two squares row by row: by their first word, then by their second word on a tie, and so on. - Return an empty list when no square can be formed. ### Constraints - `1 <= len(words) <= 1000` - `1 <= len(words[i]) <= 4` - All `words[i]` have the same length. - `words[i]` consists of lowercase English letters only. - All `words[i]` are unique. ### Examples ```text Input: words = ["area","lead","wall","lady","ball"] Output: [["ball","area","lead","lady"],["wall","area","lead","lady"]] ``` Both squares share the last three rows and differ only in the first word, and "ball" sorts before "wall". No square starts with "area" or "lead" because no word begins with "r" or "e", and "lady" followed by "area" would need a third word starting with "de". ```text Input: words = ["abat","baba","atan","atal"] Output: [["baba","abat","baba","atal"],["baba","abat","baba","atan"]] ``` "baba" appears twice in each square, which is allowed. The two squares differ only in the last row, and "atal" sorts before "atan". ```text Input: words = ["b","a"] Output: [["a"],["b"]] ``` A one-letter word is a word square by itself, so each word gives one square, listed in sorted order.

Overview: Given a list of unique words of equal length, return every word square they can form, where each row reads the same as the matching column, sorted row by row. It tests backtracking with prefix pruning and building a trie or prefix map for fast candidate lookup.

General logo
General
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given a list of unique lowercase strings words, all of the same length L, return every word square that can be built from them. The same word may be used more than once in a square.

A sequence of words forms a word square when, for every index k, the kth row and the kth column read the same string. Because every word has length L, a square has exactly L rows. For example, ["ball","area","lead","lady"] is a word square:

b a l l
a r e a
l e a d
l a d y

Row 0 and column 0 both read "ball", row 1 and column 1 both read "area", and the same holds for rows 2 and 3.

Function Signature

def word_squares(words: list[str]) -> list[list[str]]:

Rules

  • Each square is returned as a list of L words in row order, row 0 first.
  • Return the squares sorted in ascending lexicographic order, comparing two squares row by row: by their first word, then by their second word on a tie, and so on.
  • Return an empty list when no square can be formed.

Constraints

  • 1 <= len(words) <= 1000
  • 1 <= len(words[i]) <= 4
  • All words[i] have the same length.
  • words[i] consists of lowercase English letters only.
  • All words[i] are unique.

Examples

Input: words = ["area","lead","wall","lady","ball"]
Output: [["ball","area","lead","lady"],["wall","area","lead","lady"]]

Both squares share the last three rows and differ only in the first word, and "ball" sorts before "wall". No square starts with "area" or "lead" because no word begins with "r" or "e", and "lady" followed by "area" would need a third word starting with "de".

Input: words = ["abat","baba","atan","atal"]
Output: [["baba","abat","baba","atal"],["baba","abat","baba","atan"]]

"baba" appears twice in each square, which is allowed. The two squares differ only in the last row, and "atal" sorts before "atan".

Input: words = ["b","a"]
Output: [["a"],["b"]]

A one-letter word is a word square by itself, so each word gives one square, listed in sorted order.