Minimum size substring to be removed to make a given string palindromic


Palindromes are sequences of characters that read the same forwards and backwards. In computer science and programming, palindromes are a common subject for string manipulation problems. In this article, we will explore the problem of finding the minimum size substring that must be removed from a given string to make it palindromic. We will include an example to illustrate the test case.

Problem Statement

Given a string 's' of length 'n', we need to find the minimum size of the substring that should be removed to make the remaining string palindromic.

Algorithm

  • Create a function isPalindrome that takes a string 's' as an argument and returns true if it is palindromic, false otherwise.

  • Create a function minSizeSubstringToRemove that takes the string 's' as an argument.

  • Initialize a variable 'minSize' to the length of the string.

  • Iterate through the string with a loop, incrementing an index 'i' from 0 to 'n'.

  • In each iteration, perform the following steps −

    • Create two substrings: one from the beginning of the string to index 'i', and the other from index 'i' to the end of the string.

    • Check if either of the substrings is a palindrome.

    • If either substring is a palindrome, update 'minSize' to the minimum value between 'minSize' and the length of the non-palindromic substring.

  • Return 'minSize' as the result.

Example

Following are the implementations of this algorithm in various programming languages −

#include <stdio.h>
#include <string.h>

// Function to check if a string is a palindrome
int isPalindrome(const char *s, int left, int right) {
   while (left < right) {
      if (s[left] != s[right]) {
         return 0;
      }
      ++left;
      --right;
   }
   return 1;
}

// Function to find the minimum size substring to be removed
int minSizeSubstringToRemove(const char *s) {
   int n = strlen(s);
   int minSize = n;

   for (int i = 0; i <= n; ++i) {
      // Splitting the string into left and right substrings
      char leftSubstring[100];
      strncpy(leftSubstring, s, i);
      leftSubstring[i] = '\0';

      char rightSubstring[100];
      strncpy(rightSubstring, s + i, n - i);
      rightSubstring[n - i] = '\0';

      if (isPalindrome(leftSubstring, 0, i - 1)) {
         minSize = (n - i) < minSize ? (n - i) : minSize;
      }
      if (isPalindrome(rightSubstring, 0, n - i - 1)) {
         minSize = i < minSize ? i : minSize;
      }
   }
   return minSize;
}
int main() {
   char s[] = "abccbaab";
   int result = minSizeSubstringToRemove(s);
   printf("Minimum size substring to be removed: %d\n", result);

   return 0;
}

Output

Minimum size substring to be removed: 2
#include <iostream>
#include <string>
#include <algorithm>

// Function to check if a string is a palindrome
bool isPalindrome(const std::string &s) {
   int left = 0;
   int right = s.length() - 1;
   
   while (left < right) {
      if (s[left] != s[right]) {
         return false;
      }
      ++left;
      --right;
   }
   
   return true;
}

// Function to find the minimum size substring to be removed
int minSizeSubstringToRemove(std::string s) {
   int n = s.length();
   int minSize = n;
   
   for (int i = 0; i <= n; ++i) {
      std::string leftSubstring = s.substr(0, i);
      std::string rightSubstring = s.substr(i, n - i);
   
      if (isPalindrome(leftSubstring)) {
         minSize = std::min(minSize, static_cast<int>(rightSubstring.length()));
      }
      if (isPalindrome(rightSubstring)) {
         minSize = std::min(minSize, static_cast<int>(leftSubstring.length()));
      }
   }
   
   return minSize;
}

int main() {
   std::string s = "abccbaab";
   int result = minSizeSubstringToRemove(s);
   std::cout << "Minimum size substring to be removed: " << result << std::endl;
   
   return 0;
}

Output

Minimum size substring to be removed: 2
public class Main {
   // Function to check if a string is a palindrome
   private static boolean isPalindrome(String s, int left, int right) {
      while (left < right) {
         if (s.charAt(left) != s.charAt(right)) {
            return false;
         }
         left++;
         right--;
      }
      return true;
   }

   // Function to find the minimum size substring to be removed
   private static int minSizeSubstringToRemove(String s) {
      int n = s.length();
      int minSize = n;

      for (int i = 0; i <= n; i++) {
         String leftSubstring = s.substring(0, i);
         String rightSubstring = s.substring(i, n);

         if (isPalindrome(leftSubstring, 0, i - 1)) {
            minSize = Math.min(minSize, n - i);
         }
         if (isPalindrome(rightSubstring, 0, n - i - 1)) {
            minSize = Math.min(minSize, i);
         }
      }

      return minSize;
   }

   public static void main(String[] args) {
      String s = "abccbaab";
      int result = minSizeSubstringToRemove(s);
      System.out.println("Minimum size substring to be removed: " + result);
   }
}

Output

Minimum size substring to be removed: 2
# Function to check if a string is a palindrome
def is_palindrome(s, left, right):
   while left < right:
      if s[left] != s[right]:
         return False
      left += 1
      right -= 1
   return True
# Function to find the minimum size substring to be removed
def min_size_substring_to_remove(s):
   n = len(s)
   min_size = n

   for i in range(n + 1):
      left_substring = s[:i]
      right_substring = s[i:]

      if is_palindrome(left_substring, 0, i - 1):
         min_size = min(min_size, n - i)
      if is_palindrome(right_substring, 0, n - i - 1):
         min_size = min(min_size, i)

   return min_size

s = "abccbaab"
result = min_size_substring_to_remove(s)
print("Minimum size substring to be removed:", result)

Output

Minimum size substring to be removed: 2

Testcase Example

Consider the following string: "abccbaab". The possible substrings and their respective palindromic status are as follows −

  • Left substring = "", Right substring = "abccbaab", Palindromic = false

  • Left substring = "a", Right substring = "bccbaab", Palindromic = false

  • Left substring = "ab", Right substring = "ccbaab", Palindromic = false

  • Left substring = "abc", Right substring = "cbaab", Palindromic= false

  • Left substring = "abcc", Right substring = "baab", Palindromic = false

  • Left substring = "abccb", Right substring = "aab", Palindromic = true (left substring)

  • Left substring = "abccba", Right substring = "ab", Palindromic = true (left substring)

  • Left substring = "abccbaa", Right substring = "b", Palindromic = false

  • Left substring = "abccbaab", Right substring = "", Palindromic = false

From the iterations above, we can see that the minimum size substring to be removed is 2, which occurs when the left substring is "abccba" and the right substring is "ab". In this case, removing the right substring "ab" would make the remaining string "abccba" palindromic.

Conclusion

In this article, we explored the problem of finding the minimum size substring that must be removed to make a given string palindromic. We provided a clear and efficient C++ implementation that utilizes a simple loop to iterate through the string, creating substrings and checking their palindromic status to find the minimum size of the substring that must be removed.

By understanding this algorithm, you can apply similar concepts to solve other string manipulation and palindrome problems in computer science and programming.

Updated on: 27-Oct-2023

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements