*459. Repeated Substring Pattern (O(n^2)) two pointers could be better?

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"

Output: True

Explanation: It's the substring "ab" twice.

 

Example 2:

Input: "aba"

Output: False

 

Example 3:

Input: "abcabcabcabc"

Output: True

Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

 

Solution: O(n^2)

create different gaps. Hard part is to check if they are repeated substring with fixed number (boundary cased needed to be care)

class Solution {
    boolean check(String s, int i){//check each gap
        int j = 0;
        String temp1 = s.substring(j,j+i); // define temp1
        while(j+i<s.length()){
                //System.out.println(temp1+j+i);
                j = j+i;//move the gap distance
                if(j+i>s.length()) break;
                String temp2 = s.substring(j, j+i);//compare the String
                if(!temp1.equals(temp2)) return false;
                temp1 = temp2;
                //if(j+i==s.length()) break;
            }
        if(j+i==s.length()) return true;
        return false;
    }
    public boolean repeatedSubstringPattern(String s) {
        if(s==null || s.length() == 1) return false; 
        for(int i = 1; i<=s.length()/2; i++){
            if(s.length()%i!=0) continue;
            //System.out.println(i);
            //int j = 0;
            if(check(s, i)) return true;
        }
        return false;
    }
}

In a String, check if "ababab" 's substring has repested pattern

compare each character.

class Solution {
    boolean check(String s, int i){//check each gap
        int j = 0;
        while(j+i < s.length()){
            if(s.charAt(j) != s.charAt(j+i)) return false;//copare each characters !!!!!!!!!!!!!!!
            j++;
        }
        return true;
    }
    public boolean repeatedSubstringPattern(String s) {
        if(s==null || s.length() == 1) return false; 
        for(int i = 1; i<=s.length()/2; i++){
            if(s.length()%i!=0) continue;
            //System.out.println(i);
            //int j = 0;
            if(check(s, i)) return true;
        }
        return false;
    }
}

Solution 3 (O(n)) using build in function and simple tricks

public boolean repeatedSubstringPattern(String s) {
        StringBuilder str = new StringBuilder(s + s); //concatenate the given string with itself
        str.deleteCharAt(0); //remove first char
        str.deleteCharAt(str.length() - 1); //remove last char

        return str.indexOf(s) !=-1? true: false; //check if the given string is substring of the new string
    }

 

 

 

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"

Output: True

Explanation: It's the substring "ab" twice.

 

Example 2:

Input: "aba"

Output: False

 

Example 3:

Input: "abcabcabcabc"

Output: True

Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
posted @ 2018-06-02 10:41  wz30  阅读(204)  评论(0编辑  收藏  举报