Quick Overview

Given a lowercase string, return the length of its longest block of consecutive identical characters. This easy string problem from a machine learning engineering internship online assessment tests careful iteration, handling a run that ends at the last character, and telling adjacent repeats apart from scattered ones.

Length of the Longest Run of One Repeated Character in a String

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Online Assessment

Given a string, return the length of its longest run: the longest stretch of consecutive positions that all hold the same character. ### Function Signature ```python def longest_run(s: str) -> int: ``` ### Rules - A run is a non-empty block of consecutive, identical characters, for example `"ccc"` inside `"abccca"`. - Equal characters that are not adjacent never join into one run: `"abab"` consists of four runs of length 1, so its answer is `1`. - Return the length of the longest run as an integer. When several runs share the maximum length, that length is the answer. ### Constraints - `1 <= len(s) <= 10^5` - `s` contains only lowercase English letters `a` to `z`. - The answer is always between `1` and `len(s)`. ### Examples **Example 1** ```text Input: s = "aabbbcc" Output: 3 ``` The runs are `"aa"`, `"bbb"` and `"cc"`; the longest is `"bbb"`. **Example 2** ```text Input: s = "mississippi" Output: 2 ``` The string contains four `s` characters, but they form two separate runs. The runs `"ss"`, `"ss"` and `"pp"` all have length 2, and no run is longer. **Example 3** ```text Input: s = "xyzzzz" Output: 4 ``` The longest run, `"zzzz"`, ends at the last character of the string.

Overview: Given a lowercase string, return the length of its longest block of consecutive identical characters. This easy string problem from a machine learning engineering internship online assessment tests careful iteration, handling a run that ends at the last character, and telling adjacent repeats apart from scattered ones.

Given a string `s`, return the length of its longest run: the longest stretch of consecutive positions that all hold the same character. Rules: - A run is a non-empty block of consecutive, identical characters, for example `"ccc"` inside `"abccca"`. - Equal characters that are not adjacent never join into one run: `"abab"` consists of four runs of length 1, so its answer is `1`. - Return the length of the longest run as an integer. When several runs share the maximum length, that length is the answer. Implement `longest_run(s)`, which takes the string `s` and returns an integer. Constraints: - `1 <= len(s) <= 10^5` - `s` contains only lowercase English letters `a` to `z`. - The answer is always between `1` and `len(s)`, so it is at most 100,000 and never exceeds 2^31 - 1 (a 32-bit `int` holds it in every language). Example 1: Input: s = "aabbbcc" Output: 3 Explanation: The runs are "aa", "bbb" and "cc"; the longest is "bbb". Example 2: Input: s = "mississippi" Output: 2 Explanation: The string contains four `s` characters, but they form two separate runs. The runs "ss", "ss" and "pp" all have length 2, and no run is longer.

Constraints

  • 1 <= len(s) <= 10^5
  • s contains only lowercase English letters 'a' to 'z'.
  • The answer is always between 1 and len(s), so it is at most 100,000 and fits in a 32-bit signed integer.

Examples

Input: ('a',)

Expected Output: 1

Explanation: Minimum valid input: a single character is one run of length 1 (string length 1).

Input: ('az',)

Expected Output: 1

Explanation: Two different characters form two runs of length 1 (string length 2).

Hints

  1. A run only grows while each character equals the one directly before it; a letter that matches an earlier, non-adjacent letter belongs to a different run ("abab" has four runs of length 1).
  2. Several runs may share the maximum length; you only return that length, not which run achieves it.
  3. Make sure a run that reaches the very last character of the string is counted, as in "xyzzzz".

Loading coding console...

Show the approach

Approach

Scan the string once from left to right while tracking two numbers: cur, the length of the run that ends at the current position, and best, the longest run length seen so far. Both start at 1 for the first character. For each later position i, if s[i] equals s[i - 1] the current run extends, so cur grows by 1 and best becomes max(best, cur); otherwise a new run starts at i and cur resets to 1.

Invariant: after processing position i, cur is the length of the maximal run that ends at i, and best is the length of the longest run inside s[0..i]. Every run ends at some position; when the scan reaches it, cur equals that run's full length and best has already absorbed it. So after the last position, best is the length of the longest run in s.

Because best is updated as soon as cur grows, a run that reaches the last character needs no separate flush after the loop. Only neighbouring characters are compared, so equal letters separated by other letters never merge into one run. Ties need no special handling because only the length is returned.

Edge cases: a single character returns 1; a string of one repeated letter returns len(s); a string in which every pair of neighbours differs returns 1. The answer is at most 100,000, so a 32-bit int suffices in every language. The references also return 0 for an empty string, which lies outside the stated domain.

Time complexity:
O(n)
Space complexity:
O(1)