# 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 provide a well-structured C++ solution and 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

#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


## 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: 18-May-2023

158 Views