FastPrepMinimum Additions to Make a Valid ABC String
Problems/Minimum Additions to Make a Valid ABC String

Minimum Additions to Make a Valid ABC String

IBM logoIBM● MediumINTERNOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source omits the input bound and callable signature. The judged core task matches the report at about 97%.

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

  • Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
  • Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
  • Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public int addMinimum(String word) {
        // write your code here
    }

}
Checking account…