Minimum Additions to Make Valid String

Given a string word to which you can insert letters "a", "b", or "c" anywhere and any number of times, return the minimum number of letters that must be inserted so that word becomes valid.

A string is called valid if it can be formed by concatenating the string "abc" several times.

Example 1
Inputword = "b"
Output2
Insert the letter "a" right before "b", and the letter "c" right next to "b" to obtain the valid string "abc".
Example 2
Inputword = "aaa"
Output6
Insert letters "b" and "c" next to each "a" to obtain the valid string "abcabcabc".

Constraints

  • 1 <= word.length <= 50
  • word consists of letters "a", "b" and "c" only.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate