Facebook
From AK, 3 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 165
  1. class Solution:
  2.     def addMinimum(self, word: str) -> int:
  3.         word2 = []
  4.         if word[0] == 'a':
  5.             operations = 0
  6.        
  7.         elif word[0] == 'b':
  8.             operations = 1
  9.             word2.append('a')
  10.        
  11.         elif word[0] == 'c':
  12.             operations = 2
  13.             word2.append('a')
  14.             word2.append('b')
  15.  
  16.        
  17.         for i in word:
  18.             if len(word2) == 0:
  19.                 word2.append(i)
  20.                 continue
  21.            
  22.             p = word2[-1]
  23.             if (p == 'a' and i == 'b') or (p == 'b' and i == 'c') or (p == 'c' and i == 'a'):
  24.                 word2.append(i)
  25.                 continue
  26.            
  27.             elif p == i:
  28.                 word2.append(i)
  29.                 operations += 2
  30.            
  31.             elif (p == 'a' and i == 'c') or (p == 'b' and i == 'a') or (p == 'c' and i == 'b'):
  32.                 word2.append(i)
  33.                 operations += 1
  34.        
  35.         if word2[-1] == 'a':
  36.             operations += 2
  37.         elif word2[-1] == 'b':
  38.             operations += 1
  39.        
  40.         return operations