You are given an integer array matchsticks where matchsticks[i] is the length of the ith matchstick. You want to use all the matchsticks to make one square. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Return true if you can make this square and false otherwise.
Example 1:
Input: matchsticks = [1,1,2,2,2] Output: true Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: matchsticks = [3,3,3,3,4] Output: false Explanation: You cannot find a way to form a square with all the matchsticks.
Constraints:
1 <= matchsticks.length <= 151 <= matchsticks[i] <= 108In #473 Matchsticks to Square, the goal is to determine whether a set of matchsticks can form a square using every stick exactly once. The key observation is that a square has four equal sides, so the total length of all matchsticks must be divisible by 4. Once the target side length is known, the problem becomes distributing matchsticks into four groups that each sum to this value.
A common strategy is backtracking. Sort the matchsticks in descending order and try to assign each stick to one of the four sides while ensuring the side length never exceeds the target. This pruning significantly reduces unnecessary exploration. Another optimized approach uses bitmask dynamic programming, where a bitmask represents which matchsticks have been used and the current partial side length.
The backtracking method typically runs in about O(4^n) in the worst case but performs well with pruning, while the bitmask DP approach runs around O(n * 2^n) time with O(2^n) space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Backtracking with pruning | O(4^n) | O(n) |
| Bitmask Dynamic Programming | O(n * 2^n) | O(2^n) |
NeetCode
Use these hints if you're stuck. Try solving on your own first.
Treat the matchsticks as an array. Can we split the array into 4 equal parts?
Every matchstick can belong to either of the 4 sides. We don't know which one. Maybe try out all options!
For every matchstick, we have to try out each of the 4 options i.e. which side it can belong to. We can make use of recursion for this.
We don't really need to keep track of which matchsticks belong to a particular side during recursion. We just need to keep track of the <b>length</b> of each of the 4 sides.
When all matchsticks have been used we simply need to see the length of all 4 sides. If they're equal, we have a square on our hands!
This approach involves using backtracking to attempt fitting matchsticks into four equal-length sides. First, calculate the potential side length as the total sum of matchsticks divided by four. Each matchstick will be assigned to one of the four sides iteratively. If a proper combination is found, a square can be formed.
The matchsticks array is sorted in descending order to optimize the process, ensuring that larger matchsticks are placed first. This reduces the number of recursive calls by filling up spaces more quickly.
Time Complexity: O(4^N), where N is the length of matchsticks and 4 is because each matchstick could theoretically fit into one of four sides.
Space Complexity: O(N) due to the recursion stack.
1import java.util.Arrays;
2
3public class Solution {
4 public boolean makesquare(int[] matchsticks) {
5 if (matchsticks.length < 4) return false;
6 int total = Arrays.stream(matchsticks).sum();
7 if (total % 4 != 0) return false;
8 int side = total / 4;
9 Arrays.sort(matchsticks);
10 reverse(matchsticks);
11 int[] sides = new int[4];
12 return backtrack(matchsticks, sides, 0, side);
13 }
14
15 private boolean backtrack(int[] matchsticks, int[] sides, int index, int side) {
16 if (index == matchsticks.length) {
17 return sides[0] == side && sides[1] == side && sides[2] == side && sides[3] == side;
18 }
19
20 for (int i = 0; i < 4; i++) {
21 if (sides[i] + matchsticks[index] <= side) {
22 sides[i] += matchsticks[index];
23 if (backtrack(matchsticks, sides, index + 1, side)) {
24 return true;
25 }
26 sides[i] -= matchsticks[index];
27 }
28 }
29
30 return false;
31 }
32
33 private void reverse(int[] nums) {
34 int i = 0, j = nums.length - 1;
35 while (i < j) {
36 int temp = nums[i];
37 nums[i] = nums[j];
38 nums[j] = temp;
39 i++;
40 j--;
41 }
42 }
43}In this Java implementation, we first sort and reverse the matchsticks array to prioritize larger matchsticks. Recursive backtracking is used to test all possible combinations of distributing matchsticks among the four sides, aiming for each side to equal a quarter of the total length.
This approach leverages dynamic programming combined with bitmasking to efficiently explore combinations of matchsticks placed into sides. The DP state represents which matchsticks have been used, and the transition involves trying to extend the current side or start a new side once the correct length is reached.
Time Complexity: O(2^N * N), where N is the number of matchsticks, due to exploring all states.
Space Complexity: O(2^N) for memoization.
1var makesquare = function(matchsticks) {
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Sorting the matchsticks in descending order helps place longer sticks first. This increases the chances of hitting invalid states earlier, allowing the algorithm to prune branches quickly and reduce the search space.
Yes, variations of this problem appear in technical interviews at large tech companies. It tests understanding of backtracking, pruning strategies, and sometimes bitmask dynamic programming for subset partitioning problems.
A common optimal strategy uses backtracking with pruning. By sorting matchsticks in descending order and trying to build four sides with a fixed target length, many invalid states can be skipped early, making the search much faster.
Arrays are typically used to store matchstick lengths, while recursion or a bitmask representation tracks which sticks are used. In the DP approach, a bitmask is particularly useful for efficiently representing subsets of matchsticks.
The JavaScript solution mirrors the C++ dynamic programming approach with bitmasking. A map is used to memoize results for different states, indicated by the binary mask of used matchsticks. The logic attempts to add matchsticks to form sides of the expected length.