问题描述
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.
**思路:知道js中的indexOf()方法就够了(返回某个指定的字符串值在字符串中首次出现的位置),扩展lastIndexOf()以及 String直接赋字符串和new String的区别
**
*
/**
* @param {string} A
* @param {string} B
* @return {number}
*/
var repeatedStringMatch = function(A, B) {
var n = B.length/A.length;
var str = "";
for(var i = 1; i<n+5; i++){
str += A;
if(str.indexOf(B)>=0){
return i;
}
}
return -1;
};
文末彩蛋
爸爸带大的孩子更聪明啊