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.
class Solution: # Time: O(4^n) backtracking in the worst case (pruned heavily in practice) # Space: O(n) recursion stack def makesquare(self, matchsticks: list[int]) -> bool: total = sum(matchsticks) if total % 4 != 0: return False side = total // 4 # Sort descending so larger sticks fail fast and prune the search early. sticks = sorted(matchsticks, reverse=True) if sticks[0] > side: return False sides = [0, 0, 0, 0] def backtrack(index: int) -> bool: if index == len(sticks): return all(s == side for s in sides) stick = sticks[index] for i in range(4): if sides[i] + stick > side: continue # Skip duplicate side fills to avoid symmetric permutations. if i > 0 and sides[i] == sides[i - 1]: continue sides[i] += stick if backtrack(index + 1): return True sides[i] -= stick return False return backtrack(0)