class Solution: def addMinimum(self, word: str) -> int: word2 = [] if word[0] == 'a': operations = 0 elif word[0] == 'b': operations = 1 word2.append('a') elif word[0] == 'c': operations = 2 word2.append('a') word2.append('b') for i in word: if len(word2) == 0: word2.append(i) continue p = word2[-1] if (p == 'a' and i == 'b') or (p == 'b' and i == 'c') or (p == 'c' and i == 'a'): word2.append(i) continue elif p == i: word2.append(i) operations += 2 elif (p == 'a' and i == 'c') or (p == 'b' and i == 'a') or (p == 'c' and i == 'b'): word2.append(i) operations += 1 if word2[-1] == 'a': operations += 2 elif word2[-1] == 'b': operations += 1 return operations