Day 20. Repeated String Match(686)

问题描述
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;
};

文末彩蛋
爸爸带大的孩子更聪明啊


小犀牛的礼物
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容