Zepruv › Coding problems

Minimum Length of String After Deleting Similar Ends

Medium · Two Pointers, String

Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:
Return the minimum length of s after performing the above operation any number of times (possibly zero times).

Example 1

Input: s = "ca"
Output: 2

You can't remove any characters, so the string stays as is.

Example 2

Input: s = "cabaabac"
Output: 0

An optimal sequence of operations is: - Take prefix = "c" and suffix = "c" and remove them, s = "abaaba". - Take prefix = "a" and suffix = "a" and remove them, s = "baab". - Take prefix = "b" and suffix = "b" and remove them, s = "aa". - Take prefix = "a" and suffix = "a" and remove them, s = "".

Example 3

Input: s = "aabccabba"
Output: 3

An optimal sequence of operations is: - Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb". - Take prefix = "b" and suffix = "bb" and remove them, s = "cca".

Constraints

Starter code (python)

class Solution(object):
    def minimumLength(self, s):
        """
        :type s: str
        :rtype: int
        """
        

Related problems

Previous: Maximum Absolute Sum of Any Subarray · Next: Change Minimum Characters to Satisfy One of Three Conditions · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.