FastPrepMinimum Additions to Make a Valid ABC String

Minimum Additions to Make a Valid ABC String

IBM logoIBMMediumINTERNOA
Learn

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) → int

Examples

Example 1

word = "b"return = 2

Insert 'a' before 'b' and 'c' after it to obtain "abc".

Example 2

word = "aaa"return = 6

Each 'a' starts a separate cycle, so insert 'b' and 'c' after each one to obtain "abcabcabc".

Example 3

word = "abcabc"return = 0

The input already consists of two complete "abc" cycles.

Constraints

  • 1 <= word.length <= 10^5.
  • Every character in word is 'a', 'b', or 'c'.

More IBM problems

See IBM hiring insights
public int addMinimum(String word) {
    // write your code here
}
word"b"
expected2
Checking account…