Java easy solution 18ms


  • 0
    W
    public boolean repeatedSubstringPattern(String str) {
        int length = str.length();
        for (int i = 1; i <= length / 2; i++) {
            if (length % i == 0) {
                String pattern = str.substring(0, i);
                int j = i;
                while (j < length) {
                    if (!str.startsWith(pattern, j)) break;
                    j += i;
                }
                if (j == length) return true;
            }
        }
        return false;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.