Minimum Length of String After Deleting Similar Ends
Remove runs of the same letter from both ends of a string, again and again, and find how short it can get.
You are given a string s
Example 1
Output: 2
Explanation: the string starts with c and ends with a, so no move is possible.
Example 2
Output: 0
Explanation: remove c from both ends, then a, then b, and then the two a's left in the middle.
Example 3
Output: 3
Explanation: remove "aa" from the start and "a" from the end, then "b" and "bb", leaving "cca".
The Idea
Think of the string as a row of letter tiles. You may take tiles off both ends at the same time, as long as every tile you take shows the same letter. A move is only possible when the first tile and the last tile match, so only the two ends of the row ever matter.
Take Example 3, "aabccabba"
So each move takes the whole run of the letter from both ends, where a run is a stretch of the same letter in a row. Look at the picture below, which follows Example 3 from the top row down. The marker leftright
In the top row, leftright"cca"
Notice that nothing was actually deleted. The two markers simply walked toward each other, and the tiles between them are what is left of the string. Moving two markers through the data like this is called the two-pointer technique;
Solution Approach
Turning that idea into code means settling three things: how to keep track of what is left of the string, how far each marker moves in one move, and when to stop.
Tracking what is left
Why not cut the ends off the string itself, with substring"abcabccbacba"leftright
How far each marker moves
Where does a move end? leftrightleftright
That check allows leftright"aaa"leftright
When to stop
When is no move left? Either the letters at leftright"aba"leftright
The length that is left is right - left + 1leftright
Java Solution
The method minimumLength
- First, put
on index 0 andleft on the last index,right .s.length() - 1 - An outer loop makes one move per round. It runs while
is beforeleft and the letters at both markers are equal. Inside, save the letter atright inleft .c - A first inner loop moves
right past everyleft at the start, as long asc .left <= right - A second inner loop moves
left past everyright at the end, with the same check.c - When the outer loop stops, return
.right - left + 1
The main"aaa""aba""a"
public class MinimumLengthOfString {
public static int minimumLength(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right && s.charAt(left) == s.charAt(right)) {
char c = s.charAt(left);
while (left <= right && s.charAt(left) == c) {
left++; // remove the whole run of c at the start
}
while (left <= right && s.charAt(right) == c) {
right--; // and the whole run of c at the end
}
}
return right - left + 1; // 0 when the markers have crossed
}
public static void main(String[] args) {
String[] tests = {"ca", "cabaabac", "aabccabba", "aaa", "aba", "a"};
for (String s : tests) {
System.out.println("\"" + s + "\" -> " + minimumLength(s));
}
}
}
Running it prints:
"ca" -> 2
"cabaabac" -> 0
"aabccabba" -> 3
"aaa" -> 0
"aba" -> 1
"a" -> 1
Look at "cabaabac" -> 0leftrightleftright"aaa" -> 0
Both lines depend on the <=left < right
The last two lines come from the outer loop's check. "aba" -> 1"a" -> 1leftright
Complexity
The markers only ever move toward each other, so the work is a single pass over the string.
- Time: O(n), where n is the length of the string.
only moves right andleft only moves left, so together they take at most n steps before they cross.right - Space: O(1). No new string is built; the method keeps two indexes and the current letter.