Minimum Additions to Make a Valid ABC String
Problem statement
Given a string word containing only 'a', 'b', and 'c', return the minimum number of characters that must be inserted so that the resulting string is valid.
A string is valid when it is formed by concatenating one or more copies of "abc". You may insert a character at any position, but the existing characters must remain in their original order.
Function
addMinimum(word: String) → intExamples
Example 1
word = "b"return = 2Insert 'a' before 'b' and 'c' after it to obtain "abc".
Example 2
word = "aaa"return = 6Each 'a' starts a separate cycle, so insert 'b' and 'c' after each one to obtain "abcabcabc".
Example 3
word = "abcabc"return = 0The input already consists of two complete "abc" cycles.
Constraints
1 <= word.length <= 10^5.- Every character in
wordis'a','b', or'c'.