Asleep on the Keyboard
Limits: 2 sec., 256 MiB
Zenyk was writing an essay and fell asleep right on the keyboard. On waking up, Zenyk found long runs of identical letters in the text.
You are given a string \(s\) of length \(n\) consisting of lowercase Latin letters. Find the length of the longest substring all of whose letters are the same. A substring is several consecutive letters of the string.
Input
The first line contains one integer \(n\) – the length of the string.
The second line contains a string \(s\) of \(n\) lowercase Latin letters.
Output
In the only line, print one integer – the length of the longest substring of identical letters.
Constraints
\(1 \le n \le 10^5\).
The scoring consists of the following blocks:
1 point for each example from the statement,
10 points: all letters of the string are the same,
20 points: \(n \le 100\),
30 points: \(n \le 5000\),
38 points: no additional constraints.
You get the points for a block only if your program passes all tests of the block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 10 aabbbbacca | 4 |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 abc | 1 |
Notes
In the first example, the longest substring of identical letters is
bbbb, from the third to the sixth letter. Note that the
letter a occurs in the string four times, but not
consecutively.
In the second example, all adjacent letters are different, so the answer is 1.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Заснув на клавіатурі - Розбір
Пройдемо рядком зліва направо й підтримуватимемо \(cur\) — довжину серії однакових літер, яка закінчується на поточній позиції. Якщо поточна літера збігається з попередньою, серія продовжується і \(cur\) збільшується на 1, інакше починається нова серія і \(cur = 1\). Відповідь — найбільше значення \(cur\) за весь прохід.
Відповідь треба оновлювати на кожному кроці, а не лише тоді, коли літера змінюється, інакше загубиться остання серія рядка.
Складність розв’язку — \(O(n)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|