Matchsticks to Square LeetCode solution in Python
1class Solution:2def makesquare(self, matchsticks: List[int]) -> bool:3target, m = divmod(sum(matchsticks), 4)4if m:5return False6targetLst = [0] * 47length = len(matchsticks)8matchsticks.sort(reverse=True)910def bt(i):11if i == length:12return len(set(targetLst)) == 113for j in range(4):14if matchsticks[i] + targetLst[j] > target:15continue16targetLst[j] += matchsticks[i]17if bt(i + 1):18return True19targetLst[j] -= matchsticks[i]20if not targetLst[j]:21break22return False2324return matchsticks[0] <= target and bt(0)