classSolution{publicbooleanmakesquare(int[]matchsticks){if(matchsticks.length<4)returnfalse;finalintperimeter=Arrays.stream(matchsticks).sum();if(perimeter%4!=0)returnfalse;int[]edges=newint[4];Arrays.fill(edges,perimeter/4);Arrays.sort(edges);// can't do "Arrays.sort(edges, (a, b) -> b - a);" in Javareturndfs(matchsticks,matchsticks.length-1,edges);}privatebooleandfs(int[]matchsticks,intselected,int[]edges){if(selected==-1)returnArrays.stream(edges).allMatch(edge->edge==0);for(inti=0;i<4;++i){if(matchsticks[selected]>edges[i])continue;edges[i]-=matchsticks[selected];if(dfs(matchsticks,selected-1,edges))returntrue;edges[i]+=matchsticks[selected];}returnfalse;}}