> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/1001-2000/1446.-lian-xu-zi-fu.md).

# 1446.连续字符

[原题](https://leetcode-cn.com/problems/consecutive-characters/)

## 一、遍历

一次遍历，用一个计数器cnt记录当前连续字符长度，每次`s[i]` 与 `s[i-1]`比较，若相等则cnt++，否则cnt置1。每次得到新的cnt更新全局最大长度res

```python
class Solution:
    def maxPower(self, s: str) -> int:
        n = len(s)
        if n == 1:
            return 1
        cnt = 1
        res = 0
        for i in range(1, n):
            if s[i-1] == s[i] :
                cnt += 1
            else:
                cnt = 1
            res = max(res, cnt)
        return res
```
