Is the 'sample 4 ms submission' has bug?


  • 0
    C

    Hi all,
    I finished my correct submission, but it's runtime just beats 80.60 % of java submissions.
    So, I test the best beats 'sample 4 ms submission'.

    public int repeatedStringMatch(String A, String B) {
            int lenA = A.length();
            int lenB = B.length();
            
            char[] charA = A.toCharArray();
            char[] charB = B.toCharArray();
            
            int ai = 0;
            int bi = 0;
            
            while(ai<lenA && bi < lenB){
                if(charA[ai] == charB[bi]){
                    ai++;
                    bi++;
                }else{
                    ai++;
                    bi = 0;
                }
            }
            
            if(bi == 0)
                return -1;
            
            int count = 1;
            ai = 0;
            
            while(bi < lenB){
                if(ai==0)
                    count++;
                if(charA[ai] == charB[bi]){
                    ai++;
                    bi++;
                }else{
                    return -1;
                }
                if(ai == lenA)
                    ai = 0;
            }
            return count;
        }
    

    But it can't pass all my test cases:

    1. a="aabbaba", b="bab" >> excep itemt = 1 >> result = -1
    2. a="aabbaba", b="babaa" >> except = 2 >> result = -1

    Is anything wrong with me?


Log in to reply
 

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