#2645

Minimum Additions to Make Valid String

specialist · 855 · lc medium +31 · verified · 51% accepted · 591 likes · top 40%

Description

Given a string word consisting of 'a', 'b', and 'c', find the minimum number of characters to insert (anywhere) so that word becomes a concatenation of one or more copies of "abc".

Example 1:

Input: word = "b"
Output: 2
Explanation: Insert the letter "a" right before "b", and the letter "c" right next to "b" to obtain the valid string "abc".

Example 2:

Input: word = "aaa"
Output: 6
Explanation: Insert letters "b" and "c" next to each "a" to obtain the valid string "abcabcabc".

Example 3:

Input: word = "abc"
Output: 0
Explanation: word is already valid. No modifications are needed.

Code

1
2
3