Skip to content

Matchsticks to Square

MediumBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

You have matchsticks of the lengths in matchsticks. Decide whether you can make a square using every matchstick exactly once. Sticks can be joined end to end but not broken.

[1, 1, 2, 2, 2]  →  True    (sides: 2, 2, 2, 1+1)
[3, 3, 3, 3, 4]  →  False

Constraints

  • 1 <= matchsticks.length <= 15
  • 1 <= matchsticks[i] <= 10^8