Easy Java Solution - same with strStr()


  • 0
    F

    same with strStr()

    public int repeatedStringMatch(String A, String B) {
            int i = 0, j = 0;
            while (i < A.length()) {
                j = 0;
                while (j < B.length() && A.charAt((i + j) % A.length()) == B.charAt(j)) {
                    j++;
                }
                if (j == B.length()) {
                    return (i + j) / A.length() + ((i + j) % A.length() == 0 ? 0 : 1);
                }
                i++;
            }
            return -1;
        }
    

Log in to reply
 

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