LeetCode Problem

686. Repeated String Match Link to LeetCode Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b​​​​​​ to be a substring of a after repeating it, return -1. Notice: string "abc" repeated 0 times is "", repeated 1 time is "abc" and repeated 2 times is "abcabc". Example 1: Input: a = "abcd", b = "cdabcdab" Output: 3 Explanation: We return 3 because by repeating a three times "abcdabcdabcd", b is a substring of it. Example 2: Input: a = "a", b = "aa" Output: 2 class Solution { public int repeatedStringMatch(String a, String b) { int result = 0; int k = 0; int i = 0, j = 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; } }