public class Solution { public static int solution(String x) { //assume entire cake can be fully split for(int i = x.length() - 1; i > 0; i--) { //max amount of slices will be the same even if we rotate the cake int t = countSlice(x.split(x.substring(i), -1)); //slice the cake and count the slices if(t != -1) return t; //smallest slice is always the most possible slice; return on first valid slice and stop } return 1; //if no slices are valid returns the lowest possible slice, aka the entire cake } public static int countSlice(String[] split) { for(String s : split) { //if not empty it means there are leftovers; Lambda would be enraged if(!s.isEmpty()) return -1; //stop and return invalid count } return split.length - 1; //valid slices; check count from split tokens } }