-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0473-matchsticks-to-square.java
40 lines (35 loc) · 1.39 KB
/
0473-matchsticks-to-square.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
boolean[] used;
public boolean makesquare(int[] matchsticks) {
used = new boolean[matchsticks.length];
int total = 0;
for (int n : matchsticks) {
total += n;
}
//Check if total of all the sides is divisible by 4 or not
if (total % 4 != 0) return false;
int side = total / 4;
return helper(matchsticks, side, 0, 0, 4);
}
boolean helper(int[] matchsticks, int targetSide, int currentSum, int index, int sides) {
//if all the sides are matching the target side length then we found a solution
if (sides == 0)
return true;
//Check if current side is equal to targetSide , that means we found another side
if (currentSum == targetSide) {
return helper(matchsticks, targetSide, 0, 0, sides - 1);
}
for (int i = index; i < matchsticks.length; i++) {
//Only use matchsticks which are not used and which doesn't increase the current side more than target side
if (!used[i] && currentSum + matchsticks[i] <= targetSide) {
used[i] = true;
boolean found = helper(matchsticks, targetSide, currentSum + matchsticks[i], i + 1, sides);
if (found) {
return true;
}
used[i] = false;
}
}
return false;
}
}