leetcode【每日一题】统计重复个数 java

题干

由 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 。

示例:

输入:
s1 ="acb",n1 = 4
s2 ="ab",n2 = 2

返回:
2

想法

题干有几点要说的
字符串太长了不好表示
于是使用由 n 个连接的字符串 s 组成字符串 S,记作 S = [s,n]
也就是说这个S2 S1本质是长的字符串

获得:如果我们可以从 s2 中删除某些字符使其变为 s1,则称字符串 s1 可以从字符串 s2 获得
也就是s2得比s1大于等于 是在s2删除

(在这里吐槽一下leetcode真的傻逼
s1 S1大写小写分不清搞得我纠结了半天

题干上最后一句:
请你找出一个可以满足使[S2,M] 从 S1 获得的最大整数 M 。
注意是S2,是原本那个长长的字符串

那么就暴力匹配,看看S1里本来能有几个s2,再除以S2里s2的长度就行了

这大小写要命咯

Java代码

直接看代码

package daily;

public class GetMaxRepetitions {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        /*
         * @Description:index是当前要匹配的自负在s2的位置,count是S1里一共有多少个s2
         * @Author: ragezor
         * @Date: 2020/4/19 11:26 上午
         * @Param [s1, n1, s2, n2] 
         * @return int
         **/
    int index=0;
    int count=0;
    char[] array1=s1.toCharArray();
    char[] array2=s2.toCharArray();

    for(int i=0;i<n1;i++){
        for(int j=0;j<array1.length;j++){
            //找到一个字符就++
            if(array1[j]==array2[index]){
                index++;
            }
            //找到一个s2,index归零,count++
            if(index==array2.length){
                count++;
                index=0;
            }
        }
    }
    return  count/n2;
    }
    public static  void main(String [] args){
        GetMaxRepetitions getMaxRepetitions=new GetMaxRepetitions();
        String s1 ="acb", s2 ="ab";
       int n1 = 4,n2 = 2;
        System.out.println(getMaxRepetitions.getMaxRepetitions(s1,n1,s2,n2));

    }
}

我的leetcode代码都已经上传到我的git

发布了180 篇原创文章 · 获赞 0 · 访问量 3757

猜你喜欢

转载自blog.csdn.net/qq_43491066/article/details/105612147
今日推荐