Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 632 Bytes

1750.md

File metadata and controls

25 lines (23 loc) · 632 Bytes

1750. Minimum Length of String After Deleting Similar Ends

Solution 1 (time O(n), space O(1))

class Solution(object):
    def minimumLength(self, s):
        """
        :type s: str
        :rtype: int
        """
        n = len(s)
        ans = 0
        p1, p2 = 0, n - 1
        while p1 <= p2:
            cur_c = s[p1]
            if s[p2] != cur_c or p1 == p2:
                ans = p2 - p1 + 1
                break
            while p1 <= p2 and s[p1] == cur_c:
                p1 += 1
            while p1 <= p2 and s[p2] == cur_c:
                p2 -= 1
        return ans