FastPrepEqual-Length Character Blocks

Equal-Length Character Blocks

Deloitte logoDeloitte● EasyFULLTIMEOA
Learn

Problem statement

Given a string s containing only 'a' and 'b', split it into maximal consecutive blocks of equal characters.

You may add letters only at the beginning or end of an existing block. Return the minimum number of letters that must be added so that every block has the same length.

Function

minAdditionalLetters(s: String) → int

Examples

Example 1

s = "babaa"return = 3

The blocks have lengths 1, 1, 1, 2. Add one letter to each of the first three blocks to obtain blocks of length 2.

Example 2

s = "bbbab"return = 4

The blocks have lengths 3, 1, 1. Add two letters to each shorter block.

Example 3

s = "bbbaaabbb"return = 0

All three blocks already have length 3.

Constraints

  • 1 <= s.length <= 40,000
  • s contains only 'a' and 'b'.

More Deloitte problems

See Deloitte hiring insights
public int minAdditionalLetters(String s) {
    // write your code here
}
s"babaa"
expected3
Checking account…