Share my java solution


  • 0
    C
    public boolean repeatedSubstringPattern(String str) {
        char[] charArr = str.toCharArray();
        int len = charArr.length;
        for (int i = 0; i < len; i++) {
        	int patternLen = i + 1;
    		if(len % patternLen != 0 || patternLen == len) continue;
    		int count = 1;
    		for (int j = patternLen; j < len; j += patternLen) {
    			if(isEqualSub(charArr, 0, j, patternLen))
    				count++;
    			else
    				break;
    		}
    		if(Math.ceil(len / (double)patternLen) == count) return true;
    	}
        return false;
    }
    private boolean isEqualSub(char[] all, int patternStart, int subStart, int len){
    	for (int i = 0; i < len; i++) {
    		if(all[patternStart + i] != all[subStart + i]) return false;
    	}
    	return true;
    }

Log in to reply
 

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