[Solved] Google foobar The Cake is not a lie


I think this is a quick working not optimized solution. You basically suppose that you can have n parts. If it works you return, if not you suppose that you can have n-1 part and so one.

int result = -1;
    int len = s.length();
    for(int i = len; i > 0; i--){           
        int n = len/i;
        if( n * i == len){
            boolean valid = true;
            String part = s.substring(0,n);
            for(int j = 1; j < i; j++){

                if(!s.substring(j*n,j*n+n).equals(part)){
                    valid = false;
                    break;
                }

            }
            if(valid){
                result = i;
                break;
            }

        }


    }

4

solved Google foobar The Cake is not a lie