zoukankan      html  css  js  c++  java
  • 【leetcode】1446. Consecutive Characters

    题目如下:

    Given a string s, the power of the string is the maximum length of a non-empty substring that contains only one unique character.

    Return the power of the string.

    Example 1:

    Input: s = "leetcode"
    Output: 2
    Explanation: The substring "ee" is of length 2 with the character 'e' only.
    

    Example 2:

    Input: s = "abbcccddddeeeeedcba"
    Output: 5
    Explanation: The substring "eeeee" is of length 5 with the character 'e' only.
    

    Example 3:

    Input: s = "triplepillooooow"
    Output: 5
    

    Example 4:

    Input: s = "hooraaaaaaaaaaay"
    Output: 11
    

    Example 5:

    Input: s = "tourist"
    Output: 1

    Constraints:

    • 1 <= s.length <= 500
    • s contains only lowercase English letters.

    解题思路:送分题。

    代码如下:

    class Solution(object):
        def maxPower(self, s):
            """
            :type s: str
            :rtype: int
            """
            last = None
            res = 1
            count = 0
            for i in range(len(s)):
                if last == None:
                    last = s[i]
                    count = 1
                elif last == s[i]:
                    count += 1
                    res = max(count,res)
                else:
                    count = 1
                    last = s[i]
            return res
  • 相关阅读:
    禁止网页后退
    C# 数组排序
    SQL求往年的工资和
    手机的隐秘功能
    C#中的String类
    C#修饰符
    Application,Session,Cookie,ViewState和Cache区别
    css margin和padding的区别
    php常见的js正则表达式
    js 正则表达式基础篇
  • 原文地址:https://www.cnblogs.com/seyjs/p/13041676.html
Copyright © 2011-2022 走看看