Peel Matching Ends
Look at the first and the last character of s. While the string has at least two characters and these two are the same, remove them both and look again.
Return what is left when the peeling stops. It can be the empty string.
Example 1
s "abcxba"
returns "cx"
The outer a and a go, then b and b. Now the ends are c and x, which differ.
Example 2
s "abba"
returns ""
Both pairs match, so everything is peeled away.
Example 3
s "aba"
returns "b"
The two as go. A single b is left, and one character has no partner.
Constraints
0 <= len(s) <= 20sholds only lowercase English letters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Two pointers problems
- Sums of Every WindowBeginner
- Trim the ZerosBeginner
- Written BackwardsBeginner
- Zip Two Sorted ListsBeginner
- Best Window of Distinct NumbersEasy
- Closest Pair Sum in a Sorted ListEasy
- Count Pairs Below TargetEasy
- Long Pressed NameEasy