Skip to content

Commit 8867b76

Browse files
authored
Create Minimum Additions to Make Valid String.py
1 parent 6b682f8 commit 8867b76

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Given a string word to which you can insert letters "a", "b" or "c" anywhere and any number of times, return the minimum
3+
number of letters that must be inserted so that word becomes valid.
4+
5+
A string is called valid if it can be formed by concatenating the string "abc" several times.
6+
'''
7+
8+
class Solution:
9+
def addMinimum(self, word: str) -> int:
10+
it = 0
11+
result = 0
12+
while it < len(word) :
13+
if word[it:it+3] == "abc":
14+
it += 3
15+
elif word[it:it+2] in ["ab","ac","bc"]:
16+
result += 1
17+
it += 2
18+
else:
19+
result += 2
20+
it += 1
21+
return result
22+

0 commit comments

Comments
 (0)