[
string
greedy
]
BinarySearch 0361 Shortest String
Problem statement
https://binarysearch.com/problems/Shortest-String/
Solution
We can notice that as we still have 0 and 1 in string, we have place, where they are adjacent and we can delete them. We stop only if we out of 1 or out of 0.
Complexity
It is O(n) for time and space.
Code
class Solution:
def solve(self, s):
return abs(s.count("1") - s.count("0"))