首页 > 其他分享 >1156. 单字符重复子串的最大长度

1156. 单字符重复子串的最大长度

时间:2023-06-07 10:00:56浏览次数:50  
标签:子串 字符 charAt int text 1156 length ans

如果字符串中的所有字符都相同,那么这个字符串是单字符重复的字符串。

给你一个字符串 text,你只能交换其中两个字符一次或者什么都不做,然后得到一些单字符重复的子串。返回其中最长的子串的长度。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/swap-for-longest-repeated-character-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public int maxRepOpt1(String text) {
        if (text == null || text.length() == 0) {
            return 0;
        }
        int ans = 0;
        int[] count = new int[256];
        for (int i = 0; i < text.length(); i++) {
            count[text.charAt(i)]++;
        }
        for (int i = 0; i < text.length(); ) {
            int j = i;
            while (j < text.length() && text.charAt(i) == text.charAt(j)) {
                j++;
            }
            if ((j - i) < count[text.charAt(i)] && (i > 0 || j < text.length())) {
                ans = Math.max(ans, j - i + 1);
            }
            int k = j + 1;
            while (k < text.length() && text.charAt(k) == text.charAt(i)) {
                k++;
            }
            ans = Math.max(ans, Math.min(k - i, count[text.charAt(i)]));
            i = j;
        }

        return ans;
    }
}

标签:子串,字符,charAt,int,text,1156,length,ans
From: https://www.cnblogs.com/tianyiya/p/17462528.html

相关文章

  • 7.16 字符串格式化
    formatpublicclassHelloWorld{publicstaticvoidmain(Stringargs[]){Stringname="张三";intage=19;doublescore=8.8;Stringstr=String.format("姓名:%s,年龄:%d,成绩:%5.2f",name,age,score);......
  • 7.15 字符串的截取
    substring,经常结合indexOf,lastIndexOf使用,Stringstr="www.mldn.cn";System.out.println(str.substring(4));//4之后都截取System.out.println(str.substring(4,8));//截取4-8,和php不同,后面的参数不是截取的长度;......
  • 7.14 字符串拆分
    splitpublicclassHelloWorld{publicstaticvoidmain(Stringargs[]){Stringstr="helloworldhellomldn";//split按照指定字符串全部拆分//Stringresult[]=str.split("");//for(intx=0;x......
  • 删除字符串中相邻的重复字母
    题目:去除字符串中相邻且相等的两个字母,得到一个新字符串,并重复进行该操作,知道不能删除为止。思路:这道题首先想到的是利用循环,定义一个空的结果数组,遍历字符串的每一个元素,与结果数组中的最后一个元素比较,如果不相同,则追加该元素,反之删除数组最后一个元素。实现如下:constfilterD......
  • 7.12 字符串查找
    containsindexOf,lastIndexOf,startsWith,endWithpublicclassHelloWorld{publicstaticvoidmain(Stringargs[]){//Stringargs[]字符串数组的意思Stringstr="www.mldn.cn";System.out.println(str.contains("mldn"));//tru......
  • 7.11 字符串比较
    demo1equalsequalsIgnoreCaseStringstrA="mldn";StringstrB="MLDN";System.out.println(strA.equals(strB));System.out.println(strA.equalsIgnoreCase(strB));//不区分大小写来比较demo2compareTo字符串大小比较,com......
  • 7.10 字符串与字节
    publicclassHelloWorld{publicstaticvoidmain(Stringargs[]){//Stringargs[]字符串数组的意思Stringstr="helloworld";bytedata[]=str.getBytes();//将字符串变成字节数组for(intx=0;x<data.length;x++){data[......
  • Java 深入学习(5) —— 字符串
    String对象不可变String类中每一个看起来会修改String值的方法,实际上都是创建了一个新的String对象,以包含修改后的字符串内容。publicclassTestString{staticStringupcase(Strings){returns.toUpperCase();}publicstaticvoidmain(......
  • Java代码实现带时区时间字符串转为LocalDateTime对象
    不带时区时间字符串可以使用Java8中的DateTimeFormatter类来将字符串转换为LocalDateTime对象。下面是一个示例代码:importjava.time.LocalDateTime;importjava.time.format.DateTimeFormatter;publicclassDateTimeConversionExample{publicstaticvoidmain(String[......
  • 基于《PythonCookbook》的学习(3)——利用 Shell 通配符做字符串匹配
    fnmatch模块提供了fnmatch()和fnmatchcase()两个函数可以使用通配符模式对文本进行匹配fnmatch所完成的匹配操作有点介乎于加单的字符串方法和全功能的正则表达式之间。感觉蛮鸡肋的…:(......