Zepruv › Coding problems

Matchsticks to Square

Medium · Array, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask

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

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

You cannot find a way to form a square with all the matchsticks.

Constraints

Starter code (python)

class Solution(object):
    def makesquare(self, matchsticks):
        """
        :type matchsticks: List[int]
        :rtype: bool
        """
        

Related problems

Previous: Concatenated Words · Next: Ones and Zeroes · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.