My Simple solution


  • 0
    B
    public int repeatedStringMatch(String A, String B) {
        if((A == null && A.length() == 0) && (B != null && B.length() != 0)){
            return -1;
        }
        for(int i = 0; i <= A.length() - 1; i++){
            if(A.charAt(i) == B.charAt(0)){
                int compareResult = compare(A, i, B, 0);
                if(compareResult != -1){
                    return compareResult;
                }
            }
        }
        return -1;
    }
    
    private int compare(String A, int startA, String B, int startB){
        int counter = 1;
        int indexA = startA;
        for(int i = startB; i <= B.length() - 1; i++){
            if(indexA == A.length()){
                indexA = 0;
                counter += 1;
            }
            if(A.charAt(indexA) == B.charAt(i)){
                indexA += 1;
            }
            else{
                return -1;
            }
        }
        return counter;
    }

Log in to reply
 

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