美文网首页
连续字符

连续字符

作者: xialu | 来源:发表于2021-12-01 22:10 被阅读0次

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-characters

题目描述:

给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。
请你返回字符串的能量。

示例 1:

输入:s = "leetcode"
输出:2
解释:子字符串 "ee" 长度为 2 ,只包含字符 'e' 。

示例 2:

输入:s = "abbcccddddeeeeedcba"
输出:5
解释:子字符串 "eeeee" 长度为 5 ,只包含字符 'e' 。

示例 3:

输入:s = "triplepillooooow"
输出:5

示例 4:

输入:s = "hooraaaaaaaaaaay"
输出:11

示例 5:

输入:s = "tourist"
输出:1

代码实现:
class Solution {
    public int maxNum = 0;
    public int maxPower(String s) {
        int len = s.length();
        int num = 1;
        int idx = 0;
        while (idx < len) {
            int i = idx + 1;
            while (i < len && s.charAt(idx) == s.charAt(i)) {
                i++;
                num++;
            } 
            if (maxNum < num) maxNum = num;
            num = 1;
            idx = i;
        }
        return maxNum;
    }
}

相关文章

  • 连续字符

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/consec...

  • LeetCode 连续字符

    给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。 请你返回字符串的能量。 示...

  • TS数据结构与算法之获取字符串中连续最多的字符以及次数

    需求: 字符串中连续最多的字符,以及次数 如输入 'abbcccddeeee1234',计算得到: 连续最多的字符...

  • 最长公共字串和最长公共子序列

    最长公共字串,字符必须连续 最长公共子序列,字符不需要连续

  • 20170913

    1.给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDA...

  • 1446. 连续字符

    给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。请你返回字符串的能量。示例 ...

  • Leetcode 1446 连续字符

    1446. 连续字符[https://leetcode-cn.com/problems/consecutive-c...

  • 1446. 连续字符

    1446. 连续字符[https://leetcode.cn/problems/consecutive-chara...

  • No repeats please

    把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数.连续重复只以单个字符...

  • No repeats please

    把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数.连续重复只以单个字符...

网友评论

      本文标题:连续字符

      本文链接:https://www.haomeiwen.com/subject/isyexrtx.html