当前位置:网站首页>2022.5.25-----leetcode.467

2022.5.25-----leetcode.467

2022-06-09 12:28:00 路Lu727

  public int findSubstringInWraproundString(String p) {
        int n=p.length();
        char[] chs=p.toCharArray();
        int[] dp=new int[26];//存储以某字母为结束的子串最大长度,同时表示该子串的以该字母结束的子串数量
        dp[chs[0]-'a']=1;
        int len=1;
       for(int i=1;i<n;i++){
            //更新以当前字母结尾的满足条件的最大长度
           if((chs[i]-chs[i-1]+26)%26==1){ 
               len++;
           }else{ 
               len=1;
           }
            //每次更新,如果当前长度大于已有长度,那么以该字母为结束的子串的子串集合(同样以该字母结束)一定包含之前的集合,从而去重
           dp[chs[i]-'a']=Math.max(dp[chs[i]-'a'],len);
       }
        return Arrays.stream(dp).sum();
    }

原网站

版权声明
本文为[路Lu727]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_60466670/article/details/124975154