Minimum Trim Length String
Given a string s that contains only the characters 'a', 'b', and 'c', you may apply the following operation any number of times, including zero times:
- Pick a non-empty prefix in which every character is the same.
- Pick a non-empty suffix in which every character is the same.
- The prefix and the suffix must not overlap.
- The character in the prefix and the character in the suffix must be the same.
- Delete both the prefix and the suffix.
Return the minimum possible length of s after performing the operation any number of times.
A single remaining character cannot be removed, because removing a prefix and a suffix of the same character would require both ends to contain that character, and they would overlap at the same index. The minimum length is therefore at least 1 unless the whole string is removed.
Examples
Input: "ca"
Output: 2
Input: "cabaabac"
Output: 0
Hints
Compare the first and last characters. If they differ, no operation can remove either one, so their gap is the answer.
When the end characters match, remove the entire run of that character from both ends before comparing again.
Track the surviving window with two indices and return `right - left + 1` once the endpoints differ or the indices cross.
Minimum Trim Length String
Given a string `s` that contains only the characters `'a'`, `'b'`, and `'c'`, you may apply the following operation any number of times, including zero times: