<p>Given a string <code>word</code> to which you can insert letters &quot;a&quot;, &quot;b&quot; or &quot;c&quot; anywhere and any number of times, return <em>the minimum number of letters that must be inserted so that <code>word</code> becomes <strong>valid</strong>.</em></p>

<p>A string is called <strong>valid </strong>if it can be formed by concatenating the string &quot;abc&quot; several times.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> word = &quot;b&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> Insert the letter &quot;a&quot; right before &quot;b&quot;, and the letter &quot;c&quot; right next to &quot;b&quot; to obtain the valid string &quot;<strong>a</strong>b<strong>c</strong>&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> word = &quot;aaa&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> Insert letters &quot;b&quot; and &quot;c&quot; next to each &quot;a&quot; to obtain the valid string &quot;a<strong>bc</strong>a<strong>bc</strong>a<strong>bc</strong>&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> word = &quot;abc&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> word is already valid. No modifications are needed. 
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= word.length &lt;= 50</code></li>
	<li><code>word</code> consists of letters &quot;a&quot;, &quot;b&quot;&nbsp;and &quot;c&quot; only.&nbsp;</li>
</ul>

class Solution:
    def addMinimum(self, word: str) -> int:
        # Initialize the counter for insertions
        insertions = 0
        # Initialize a pointer to track the position in the "abc" sequence
        i = 0
        # The valid sequence we are trying to match
        valid_sequence = "abc"
        
        # Iterate over each character in the word
        for char in word:
            # If the current character does not match the expected character in "abc"
            while char != valid_sequence[i]:
                # Increment the insertion counter
                insertions += 1
                # Move to the next character in "abc"
                i = (i + 1) % 3
            # Move to the next character in "abc" after a match
            i = (i + 1) % 3
        
        # If we end in the middle of a sequence, add the remaining insertions
        if i != 0:
            insertions += (3 - i)
        
        return insertions

# Example usage:
solution = Solution()
print(solution.addMinimum("b"))  # Output: 2
print(solution.addMinimum("aaa"))  # Output: 6
print(solution.addMinimum("abc"))  # Output: 0
39