101 Logo
onenoughtone

Problem Statement

String End Trimmer

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:

  1. Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
  2. Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
  3. The prefix and the suffix should not intersect at any index.
  4. The characters from the prefix and suffix must be the same.
  5. Delete both the prefix and the suffix.

Return the minimum length of s after performing the above operation any number of times (possibly zero times).

Examples

Example 1:

Input: s = "ca"
Output: 2
Explanation: You can't remove any characters, so the string stays as is.

Example 2:

Input: s = "cabaabac"
Output: 0
Explanation: 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
Explanation: 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

  • 1 <= s.length <= 10^5
  • s only consists of characters 'a', 'b', and 'c'.

Problem Breakdown

To solve this problem, we need to:

  1. The problem can be solved using a two-pointer approach, with one pointer at the beginning and one at the end of the string
  2. If the characters at both ends are the same, we can remove them along with any consecutive identical characters
  3. We need to be careful about the case where the entire string consists of the same character
  4. The problem is essentially asking for the length of the remaining string after removing matching characters from both ends
  5. The operation is greedy - we always want to remove as many characters as possible in each step
ProblemSolutionCode
101 Logo
onenoughtone