-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmin_substring.py
54 lines (38 loc) · 1.2 KB
/
min_substring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Python3 program for the above approach
# Function to count minimum removals
# required to make a given string
# concatenation of substring of 0s
# followed by substring of 1s
def minimumDeletions(s):
# Stores the length of the string
n = len(s)
# Precompute the count of 0s
zeroCount = [0 for i in range(n)]
# Check for the last character
zeroCount[n - 1] = 1 if s[n - 1] == "0" else 0
# Traverse and update zeroCount array
for i in range(n - 2, -1, -1):
# If current character is 0,
zeroCount[i] = zeroCount[i + 1] + \
1 if (s[i] == "0") else zeroCount[i + 1]
# Keeps track of deleted 1s
oneCount = 0
# Stores the count of removals
ans = 10 ** 9
# Traverse the array
for i in range(n):
# If current character is 1
if s[i] == "1":
# Update ans
ans = min(ans, oneCount + zeroCount[i])
oneCount += 1
# If all 1s are deleted
ans = min(ans, oneCount)
# Return the minimum
# number of deletions
return zeroCount
# Driver Code
if __name__ == "__main__":
str = "00101101"
print(minimumDeletions(str))
# This code is contributed by mohit kumar 29