<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p>

<p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> matchsticks = [1,1,2,2,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> matchsticks = [3,3,3,3,4]
<strong>Output:</strong> false
<strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= matchsticks.length &lt;= 15</code></li>
	<li><code>1 &lt;= matchsticks[i] &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution:
    def makesquare(self, matchsticks: List[int]) -> bool:
        sumVal = sum(matchsticks)
        if sumVal % 4 != 0:
            return False
        edge = sumVal // 4

        matchsticks.sort(reverse=True)

        def helper(l1, l2, l3, l4, i):
           
            if l1 == edge and l2 == edge and l3 == edge and l4 == edge:
                
                return True
            if l1 > edge or l2 > edge or l3 > edge or l4 > edge:
                
                return False
            
            
            
            return helper(l1+matchsticks[i], l2, l3, l4, i+1) or helper(l1, l2+matchsticks[i], l3, l4, i+1) or helper(l1, l2, l3+matchsticks[i], l4, i+1) or helper(l1, l2, l3, l4+matchsticks[i], i+1)

        return helper(0, 0, 0, 0, 0)































        # ### Time Complexity: **O(4^N)**

        # 1. **Number of Choices per Matchstick:**
        # - For each matchstick, there are **4** possible edges it can be assigned to.
        
        # 2. **Total Combinations:**
        # - If there are **N** matchsticks, the total number of possible assignments is \(4^N\).
        
        # 3. **Pruning Optimizations:**
        # - **Sorting:** The matchsticks are sorted in descending order to place larger matchsticks first, which can lead to earlier pruning of invalid paths.
        # - **Early Termination:** The algorithm checks if any edge exceeds the target length (`edge`) and backtracks immediately if it does.
        
        # While these optimizations can significantly reduce the number of recursive calls in practice, they do not change the **worst-case** time complexity, which remains exponential.

        # ### Space Complexity: **O(N)**

        # 1. **Recursion Stack:**
        # - The depth of the recursion is proportional to the number of matchsticks, **N**, since each recursive call processes one matchstick.
        
        # 2. **Auxiliary Space:**
        # - Sorting the `matchsticks` list in place requires **O(N)** space.
        
        # 3. **Total Space:**
        # - Combining the recursion stack and the space used for sorting, the overall space complexity is **O(N)**.


        if len(matchsticks) < 4:
            return False
        value = sum(matchsticks)
        if value % 4 != 0:
            return False
        
        edge = value // 4
        matchsticks.sort(reverse=True)

        def dfs(l1, l2, l3, l4, i):
            nonlocal edge
            if l1 == edge and l2 == edge and l3 == edge and l4 == edge:
                return True
            if l1 > edge or l2 > edge or l3 > edge or l4 > edge:
                return False

            return dfs(l1+matchsticks[i], l2, l3, l4, i+1) or dfs(l1, l2+matchsticks[i], l3, l4, i+1) or dfs(l1, l2, l3+matchsticks[i], l4, i+1) or dfs(l1, l2, l3, l4+matchsticks[i], i+1)

        return dfs(0, 0, 0, 0, 0)
            



17097