4月打卡活动第19天 LeetCode第466题:统计重复个数(困难)
- 题目:由 n 个连接的字符串 s 组成字符串 S,记作 S = [s,n]。例如,[“abc”,3]=“abcabcabc”。
如果我们可以从 s2 中删除某些字符使其变为 s1,则称字符串 s1 可以从字符串 s2 获得。例如,根据定义,“abc” 可以从 “abdbec” 获得,但不能从 “acbbe” 获得。
现在给你两个非空字符串 s1 和 s2(每个最多 100 个字符长)和两个整数 0 ≤ n1 ≤ 106 和 1 ≤ n2 ≤ 106。现在考虑字符串 S1 和 S2,其中 S1=[s1,n1] 、S2=[s2,n2] 。
请你找出一个可以满足使[S2,M] 从 S1 获得的最大整数 M 。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3d17ccb85a371219fbf887e9ed62436e.png)
- 题解做法:单纯打个卡。。
class Solution {
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
if(n1==0) return 0;
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
int l1 = s1.length();
int l2 = s2.length();
int couts1=0;
int couts2=0;
int p=0;
Map<Integer,int[]> mp = new HashMap<>();
while(couts1<n1){
for(int i=0;i<l1;i++){
if(c1[i]==c2[p]){
p++;
if(p==l2){
p=0;
couts2++;
}
}
}
couts1++;
if(!mp.containsKey(p)){
mp.put(p,new int[]{couts1,couts2});
}
else{
int[] last =mp.get(p);
int circle1= couts1-last[0];
int circle2= couts2-last[1];
couts2 += circle2*((n1-couts1)/circle1);
couts1 = couts1+((n1-couts1)/circle1)*circle1;
}
}
return couts2/n2;
}
}
作者:banfeipeng
链接:https://leetcode-cn.com/problems/count-the-repetitions/solution/shuang-bai-jie-fa-qiu-zan-qiu-guan-zhu-by-banfeipe/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d23827f758415577b0135ed9cc656e88.png)