Maximum Number of Equal Length Runs

You are given a string s consisting only of lowercase English letters.

A run is defined as a maximal substring made up of the same character.

A run cannot be extended to the left or right without changing the character.

For example, in "aabbcaaa", the runs are:

  • "aa"
  • "bb"
  • "c"
  • "aaa"

You are not allowed to split or break a run.

For example, the run "aa" must be treated as a single run.

You cannot split it into "a" and "a".

Return the maximum number of runs you can select such that all selected runs have equal length.

Example 1:

Input: s = "abcde"

Output: 5

Explanation:

Each character forms a run of length 1: "a", "b", "c", "d", "e"

All runs have equal length 1, so you can select all 5.

Example 2:

Input: s = "aaabbccdd"

Output: 3

Explanation:

The runs are:

  • "aaa" (length 3)
  • "bb" (length 2)
  • "cc" (length 2)
  • "dd" (length 2)

You cannot split "aaa" into "a","a","a".

There are three runs of length 2 → "bb", "cc", "dd"

Hence, the maximum number of runs with equal length is 3.

Now Your Turn!

Pick the correct output for the given input

Input: s = "aabbccdde"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 105
  • s consists only of lowercase English letters ('a' to 'z')
  • Runs are formed by consecutive identical characters only
  • Runs are maximal and cannot be broken
  • The string is non-empty

Hints

Frequently Occurring Doubts

Fun Facts

0
class Solution {
public:
int maxSameLengthRuns(string s) {
// Your code goes here
}
};
Test Case

Input:

S