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
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.
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
- 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).
- Several runs may share the maximum length; you only return that length, not which run achieves it.
- Make sure a run that reaches the very last character of the string is counted, as in "xyzzzz".