1. The title site
https://leetcode.com/problems/repeated-string-match/
2. Title Description
3. subject to the effect
Given two strings, A and B, and so many times allows A to A B substring repeated.
4. Problem-solving ideas
- First, the length B is calculated count how many times the length of the A string, multiple count (i.e., the length of B is not fully divisible A), then the count ++
- Then define a variable of type StringBuilder, so he repeated Add to count A.
- Note: this place is easy to overlook a Class test, i.e., A = "abcd", B = "cdabcdab"
5. AC Code
class Solution {
public int repeatedStringMatch(String A, String B) {
int alen = A.length();
int blen = B.length();
int count = blen / alen;
if(blen % alen != 0) count++;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < count; i++){
sb.append(A);
}
if(!sb.toString().contains(B)){
sb.append(A);
count++;
if(!sb.toString().contains(B)) return -1;
}
return count;
}
}
6. similar source
[1] 459. Repeated Substring Pattern topic site: https://leetcode.com/problems/repeated-substring-pattern/