<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:

        chars = 'abc'
        word_idx = 0
        char_idx = 0
        result = 0

        while word_idx < len(word):
            if chars[char_idx] == word[word_idx]:
                char_idx = (char_idx + 1) % len(chars)
                word_idx += 1
            else:
                char_idx = (char_idx + 1) % len(chars)
                result += 1

        if char_idx != 0:
            return result + len(chars) - char_idx 

        return result




    def addMinimum2(self, word: str) -> int:
        result = 0
        chars = 'abc'

        i = 0
        last_char = None

        for j, c in enumerate(word):
            print(f"doing {c}, i = {i}, idx = {chars.index(c)}")
            last_char = c
            if chars[i] == c:
                print("we're good, skipping")
                i = (i + 1) % len(chars)
                continue
            
            idx = chars.index(c)

            # add crap before
            if idx > i:
                print(f"adding {idx - i} before")
                result += idx - i
                i = (idx + 1) % len(chars)
            # add crap after
            else:
                print(f"adding {len(chars) - idx - 1} after")

                result += len(chars) - idx - 1
                i = 0
                last_char = chars[-1]


        print(f"last_char was {last_char}")
        if last_char != chars[-1]:
            print(f"adding {len(chars) - chars.index(last_char) - 1} to end")

            result += len(chars) - chars.index(last_char) - 1

        return result
        
47