LeetCode – Repeated String Match (Java)

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.

For example, with A = “abcd” and B = “cdabcdab”.

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times (“abcdabcd”).

Note:
The length of A and B will be between 1 and 10000.

Java Solution

The optimal solution’s time complexity is O(n) where n is the length of the longer string from A and B.

public int repeatedStringMatch(String A, String B) {
    int i = 0;
    int j = 0;
 
    int result = 0;
    int k = 0;
 
    while (j < B.length()) {
        if (A.charAt(i) == B.charAt(j)) {
            i++;
            j++;
 
            if (i == A.length()) {
                i = 0;
                result++;
            }
        } else {
            k++;
            if (k == A.length()) {
                return -1;
            }
            i = k;
            j = 0;
            result = 0;
        }
    }
 
    if (i > 0) {
        result++;
    }
 
    return result;
}

2 thoughts on “LeetCode – Repeated String Match (Java)”

  1. i dont know about this algo. namings of vars is very horrible!
    but idea is such

    lets say we have string A – 112233, and string B which should be part of repeated A.
    , so B is part of AAA, if only if
    B is { possible suffix of A}112233’112233{possible prefix A}
    ======
    ,so lets find A inside B, lets continue mapping A into B as long as unless prefix, and-or-suffix is less than length of A! check prefix and suffix must be part of A.

Leave a Comment