0
点赞
收藏
分享

微信扫一扫

【Leetcode_easy】686. Repeated String Match

忍禁 2022-07-12 阅读 88

problem

​​686. Repeated String Match​​

solution1:

使用string类的find函数;

class Solution {
public:
int repeatedStringMatch(string A, string B) {
int n1 = A.size(), n2 = B.size(), cnt = 1;
string t = A;
while(t.size() < n2)
{
t += A;
cnt++;
}
if(t.find(B) != string::npos) return cnt;//err.
t += A;
return (t.find(B) != string::npos) ? cnt+1 : -1;//
}
};

solution2:

其实可以直接算出最多需要增加的个数,即B的长度除以A的长度再加上2,当B小于A的时候,那么可能需要两个A,所以i就是小于等于2,这样每次在t中找B,如果找到了,就返回i,没找到,就增加一个A,循环推出后返回-1即可。 

 

 

参考

1. ​​Leetcode_easy_686. Repeated String Match​​;

2. ​​Grandyang​​;

举报

相关推荐

0 条评论