686.重复叠加字符串匹配
链接:686.重复叠加字符串匹配
难度:Medium
标签:字符串、字符串匹配
简介:给定两个字符串 a 和 b,寻找重复叠加字符串 a 的最小次数,使得字符串 b 成为叠加后的字符串 a 的子串,如果不存在则返回 -1。
题解 1 - cpp
- 编辑时间:2021-12-22
- 执行用时:72ms
- 内存消耗:39.5MB
- 编程语言:cpp
- 解法介绍:判断最大重复次数是否满足 b。
function repeatedStringMatch(a: string, b: string): number {
let cnt = Math.ceil(b.length / a.length);
if (a.repeat(cnt).includes(b)) return cnt;
if (a.repeat(cnt + 1).includes(b)) return cnt + 1;
return -1;
}