Equal-Length Character Blocks
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) → intExamples
Example 1
s = "babaa"return = 3The 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 = 4The blocks have lengths 3, 1, 1. Add two letters to each shorter block.
Example 3
s = "bbbaaabbb"return = 0All three blocks already have length 3.
Constraints
1 <= s.length <= 40,000scontains only'a'and'b'.