1750. Minimum Length of String After Deleting Similar Ends

The problem statement provides a string s and requires us to find the minimum length of a palindrome that can be obtained by removing characters from the ends of the string. This essentially means finding the length of the longest palindrome prefix and suffix of the string.

Approach and Solution

The given Java solution provides a concise and efficient approach to solve the problem. Let’s break down the solution step by step:

  1. Convert the string s into a character array c to enable easy manipulation of characters.
  2. Initialize two pointers left and right at the beginning and end of the string respectively.
  3. Iterate through the string using a while loop until left is less than right. Within the loop:
    • Store the character at the left pointer in a temporary variable temp.
    • If the characters at the left and right pointers are not equal, break out of the loop since the current substring is not a palindrome.
    • Increment left until it reaches a character different from temp.
    • Decrement right until it reaches a character different from temp.
  4. Return the length of the palindrome substring, which is (right - left + 1). If the length is less than or equal to zero, return 0 since no palindrome substring exists.
class Solution {
    public int minimumLength(String s) {
        char c[] = s.toCharArray();
        int left = 0, right = c.length-1;
        while (left < right) {
           char temp = c[left];
           if (c[left] != c[right]) {
           while(left <= right && temp == c[left]) {
           while(left <= right && temp == c[right]){
        return right-left+1 <= 0 ? 0 : right-left+1;



In this article, we explored the problem of finding the minimum length of a palindrome substring in a given string. We presented a Java solution that efficiently solves the problem by iterating through the string and identifying the longest palindrome prefix and suffix. This solution offers an elegant approach to tackle similar problems involving string manipulation and pattern recognition.

Related Post