leetcode 1446. Consecutive Characters

wonderful world·2021년 12월 13일
0

leetcode

목록 보기
7/21

https://leetcode.com/problems/consecutive-characters/

Description

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

Given a string s, return the power of s.

Solution

time complexity: O(n)

class Solution:
    def maxPower(self, s: str) -> int:
        power = 1
        prev = s[0]
        curr_power = 1
        for c in s[1:]:
            if c == prev:
                curr_power += 1
            else:
                power = max(curr_power, power)
                curr_power = 1
                prev = c
    
        return max(power, curr_power)
            
profile
hello wirld

0개의 댓글