Matchsticks to Square - Complete Solution Guide
Matchsticks to Square is LeetCode problem 473, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer array matchsticks where matchsticks[i] is the length of the i th 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:
Detailed Explanation
The problem asks us to determine whether it's possible to form a square using a given set of matchsticks, without breaking any of them. Each matchstick must be used exactly once, and they can be joined at their ends. The input is an array of integers representing the lengths of the matchsticks. The output should be `true` if a square can be formed, and `false` otherwise. The constraints are that the number of matchsticks is between 1 and 15, and the length of each matchstick is between 1 and 10^8.
Solution Approach
The solution uses a backtracking algorithm to explore all possible combinations of assigning matchsticks to the four sides of the square. It first checks if the sum of the matchsticks is divisible by 4. If not, it returns `false`. Then, it calculates the target length of each side of the square. The matchsticks are sorted in descending order to prioritize larger sticks. The `backtrack` function recursively tries to add each matchstick to one of the four sides. If a matchstick can be added without exceeding the target length, the function recursively calls itself with the next matchstick. If the base case is reached where all matchsticks have been assigned, it means a square can be formed, and it returns `true`. Backtracking ensures exploration of all valid combinations by adding and removing matchsticks from the current side.
Step-by-Step Algorithm
- Step 1: Calculate the total length of all matchsticks.
- Step 2: Check if the total length is divisible by 4. If not, return `false`.
- Step 3: Calculate the target length for each side of the square (total length / 4).
- Step 4: Sort the matchsticks array in descending order.
- Step 5: Define a recursive `backtrack` function that takes the current index as input.
- Step 6: In the `backtrack` function, if the index reaches the end of the array (all matchsticks have been used), return `true`.
- Step 7: Iterate through the four sides of the square.
- Step 8: For each side, check if adding the current matchstick's length to the side's current length exceeds the target length, and skips duplicate calculations.
- Step 9: If the matchstick can be added to the side, add it and recursively call `backtrack` with the next index.
- Step 10: If the recursive call returns `true`, return `true` immediately.
- Step 11: If the recursive call returns `false`, backtrack by subtracting the matchstick's length from the side and continue to the next side.
- Step 12: If none of the sides can accommodate the current matchstick, return `false`.
- Step 13: Call the `backtrack` function with the initial index of 0 and return its result.
Key Insights
- Insight 1: The sum of all matchstick lengths must be divisible by 4 for a square to be possible. This gives us the side length of the square.
- Insight 2: Backtracking is a suitable approach to explore all possible combinations of assigning matchsticks to the four sides of the square.
- Insight 3: Sorting the matchsticks in descending order can improve efficiency by pruning the search space early. If a larger matchstick cannot fit on any of the sides, we can avoid exploring further combinations from that point.
Complexity Analysis
Time Complexity: O(4^N)
Space Complexity: O(N)
Topics
This problem involves: Array, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask.
Companies
Asked at: PhonePe, eBay.