blob: 409410dba84c003db8c10b4f200121fc08f1a1cc (
plain)
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
|
# 1750. Minimum Length of String After Deleting Similar Ends
"""
g given a string 's' consisting of only characters a, b, and c you are asked to
g apply the following algorithm on the string any number of times. first, pick
g a non-empty prefix from the string 's' where all the characters in the prefix
g are equal. next, pick a non-empty suffix from the string 's' where all the
g characters in the suffix are equal. the prefix and the suffix should not
g intersect at any index. the characters from the prefix and the suffix must be
g the same. delete both the prefix and the suffix. return the minimum length of
g 's' after performining the above operation any number of times.
"""
class Solution(object):
def minimumLength(self, s):
"""
:type s: str
:rtype: int
"""
while len(s) > 1 and s[0] == s[-1]:
s = s.strip(s[0])
return len(s)
if __name__ == "__main__":
obj = Solution()
print(obj.minimumLength("ca"))
print(obj.minimumLength("cabaabac"))
print(obj.minimumLength("aabccabba"))
|