본문 바로가기
python-algorithm

leetcode 1446. Consecutive Characters

by 무적김두칠 2023. 1. 14.

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

 

Consecutive Characters - LeetCode

Consecutive Characters - 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.   Example 1: Input: s = "leetcode" Output: 2 Explanation: The substring "ee"

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def maxPower(self, s: str-> int:
        answer = 1
        tmp_cnt = 1
        ex_chr = ''
        for current_chr in s:
            if ex_chr==current_chr:
                tmp_cnt+=1
            else:                
                tmp_cnt = 1
            answer = max(tmp_cnt,answer)
            ex_chr = current_chr
 
        return answer
cs
반응형

댓글