Sunday, September 15, 2019

459. repeated-substring-pattern

459. repeated-substring-pattern
Runtime: 15 ms, faster than 71.70% of Java online submissions for Repeated Substring Pattern.
Memory Usage: 36.5 MB, less than 100.00% of Java online submissions for Repeated Substring Pattern.
class Solution {
    public boolean repeatedSubstringPattern(String s) {
        //abcabcabcabc

        char [] arr = s.toCharArray();

        // distance
        for (int i=1; i<=arr.length/2; i++){
            if(arr.length % i !=0){
                continue;
            }
            boolean allSame = true;
            // 각 파티션
            second :
            for(int j=i; j<arr.length; j+=i){
                // 파티션의 distacne 까지 비교
                for(int k=0; k<i; k++){
                    if(arr[k] != arr[j+k]){
                        allSame = false;
                        break second;
                    }
                }
            }
            if(allSame){
                return true;
            }
        }
        return false;
    }
}

No comments:

Post a Comment