Skip to main content

Word Squares - Solution & Explanation

HardPremiumFree on FleetCodeArrayStringBacktrackingTrie5 min readAsked at: Google
Practice this problem

Problem Statement

Given an array of unique strings words, return all the word squares you can build from words. The same word from words can be used multiple times. You can return the answer in any order.

A sequence of strings forms a valid word square if the kth row and column read the 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.

 

Example 1:

Input: words = ["area","lead","wall","lady","ball"]
Output: [["ball","area","lead","lady"],["wall","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: words = ["abat","baba","atan","atal"]
Output: [["baba","abat","baba","atal"],["baba","abat","baba","atan"]]
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).

 

Constraints:

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

Approach Overview

Problem Overview: Given a list of words with the same length, build all possible word squares. A word square means the k-th row and k-th column form the same string. If the first row starts with "ball", the first column must also read "ball". The task is to generate every valid arrangement of words that satisfies this constraint.

Approach 1: Backtracking with Prefix Scanning (Brute Force) (Time: O(N^L * L), Space: O(L))

Start with each word as the first row of the square and build the rest using backtracking. At step k, derive the required prefix for the next word using the characters from column k of previously placed rows. Scan the entire word list and pick candidates whose prefix matches. Continue recursively until the square reaches size L. This approach works but repeatedly scanning the array makes prefix lookups expensive.

Approach 2: Trie + Backtracking (Optimized) (Time: O(N * L^2) average, Space: O(N * L))

Speed up candidate lookup by indexing all words in a Trie. Each node stores the list of word indices that share the prefix represented by that node. During backtracking, compute the prefix needed for the next row and query the Trie to instantly retrieve all valid candidates. This removes the repeated full-array scans and turns prefix lookup into O(L). The recursive search builds squares row by row while maintaining the row/column symmetry constraint.

The key observation: when you already placed k rows, the next word must start with the characters formed by column k. Using a Trie makes this prefix query efficient. The remaining logic is standard depth‑first exploration over candidate words.

This problem heavily combines array traversal, string prefix construction, and structured pruning using a Trie.

Recommended for interviews: Trie + backtracking is the expected solution. Brute force backtracking demonstrates understanding of the square constraint, but efficient prefix lookup using a Trie shows stronger algorithmic design and pruning strategy.

Solution

Code

Python

Java

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Backtracking with Prefix ScanningO(N^L * L)O(L)Small input sizes or when demonstrating the base backtracking idea
Trie + BacktrackingO(N * L^2) averageO(N * L)Optimal approach for interviews and large word lists with many prefix queries

Video Solution

425 Word Squares | Leetcode | Google Interview Question • Sonu Raj • 5,483 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Word Squares easy or hard?
Word Squares is a Hard difficulty problem. The challenge comes from combining backtracking with efficient prefix queries. A naive solution works conceptually but becomes slow without a Trie-based optimization.
How to solve Word Squares efficiently?
Build a Trie that maps every prefix to the list of words starting with that prefix. Use backtracking to build the square row by row. At step k, compute the prefix formed by column k and query the Trie for candidate words. This pruning ensures only valid prefixes are explored.
What is the best approach for Word Squares?
The best approach uses Trie with backtracking. Store all words in a Trie where each node tracks words sharing that prefix. During backtracking, compute the required column prefix and fetch candidates directly from the Trie. This avoids scanning the full list each time and reduces the search cost significantly.
Is Word Squares asked at Google/Amazon/Meta?
Word Squares is considered a classic hard backtracking + Trie problem and has appeared in interviews at companies like Google and Amazon. It tests recursive search, prefix pruning, and efficient string indexing structures.
What data structure is used in Word Squares?
The optimized solution uses a Trie for fast prefix lookup combined with backtracking to build valid squares. Arrays or lists store candidate words while strings are used to construct the column prefix during recursion.
What is the time complexity of Word Squares?
Trie construction takes O(N * L) where N is the number of words and L is the word length. The backtracking search typically runs around O(N * L^2) on average because prefix queries take O(L) and only matching candidates are explored. Worst-case complexity can grow exponentially if many words share the same prefixes.
Word Squares Python or Java solution approach?
Both Python and Java implementations follow the same pattern: build a Trie mapping prefixes to word indices, then perform DFS backtracking. At each level compute the required prefix and retrieve matching words from the Trie before continuing the recursion.

Ready to solve this problem?

Practice Word Squares with our built-in code editor and test cases.

Practice on FleetCode