Make a String Non-Palindromic By Inserting a Given Character


Problem Statement

We have given a string str and character c in the input. We need to insert the given character c in the string at the index so that we can convert the string to non-palindromic. If we can’t convert the string to non-palindrome, print ‘-1’.

Sample Examples

Input

str = ‘nayan’, c = ‘n’

Output

‘nnayan’

Explanation

There can be multiple output strings as we can insert ‘n’ at any index in the given string. So, the output string can be ‘nnayan’, ‘nanyan’, ‘naynan’, ‘nayann’, etc.

Input

str = ‘sss’, c = ‘s’

Output

‘-1’

Explanation

Wherever we insert the ‘s’ in the given string, it will always be a palindrome.

Input

str = ‘tutorialspoint’, c = ‘p’

Output

‘ptutorialspoint’

Explanation

As str is already non-palindromic, it prints the same string by inserting the char c at the first index.

The logic to solve the above problem is that if all characters in the given string are equal to the given character c, we can’t make it a palindrome. Otherwise, add a character at the first position and check whether the resultant string is a palindrome. If yes, insert the given character at the end.

Approach 1

In this approach, we used the while loop to check if a given string is a palindrome, and for loop, check whether all characters in the given string are the same.

Algorithm

  • Step 1 − Initialize the ‘cnt’ variable to store the count of the characters which are equal to the given character c.

  • Step 2 − Use the for loop to iterate through the string. If the character at the ith index in the string is equal to the character c, increment the value of ‘cnt’ by 1.

  • Step 3 − If the value of ‘cnt’ is equal to the string’s length, print ‘-1’ and execute the return statement.

  • Step 4 − Initialize a ‘temp’ variable with the c + str. After that, use the isPalindrome() function to check whether the given string is a palindrome.

  • Step 5 − Define the isPalindrome() function.

  • Step 5.1 − Define the ‘left’ variable and initialize it with 0. Also, define the ‘right’ variable and initialize with the value equal to the string’s length -1.

  • Step 5.2 − Use the while loop, and match characters from the start and end of the string. Also, increase the value of ‘left’ and decrease the value of the ‘right’ variable.

  • Step 5.3 − If any mismatch is found, return false; Otherwise, return true when all the loop iteration is completed.

  • Step 6 − If the value of the ‘temp’ variable is non-palindrome, print it; Otherwise, print the str + c.

Example

#include <bits/stdc++.h>
using namespace std;
// Function to check if a string is a palindrome
bool isPalindrome(string str) {
   int left = 0;
   int right = str.length() - 1;
   // Keep comparing characters while they are the same
   while (right > left) {
      if (str[left++] != str[right--]) {
         return false;
      }
   }
   return true;
}
// Function to make a string non-palindrome by adding a character
void makeNonPalindrome(string str, char c) {
   int cnt = 0;
   for (int i = 0; i < str.length(); i++) {
      if (str[i] == c) {
         cnt++;
      }
   }
   if (cnt == str.length()) {
      cout << "-1";
      cout << "We can convert the string into a non-palindromic string by adding a given character at any position.";
      return;
   }
   cout << "Non-palindromic string is: " << endl;
   // append the character at the start, and check if it is a palindrome
   string temp = c + str;
   if (!isPalindrome(temp)){
      cout << temp << endl;
   } else {
      cout << str + c << endl;
   }
}
int main(){
   string str = "sass";
   char c = 's';
   makeNonPalindrome(str, c);
   return 0;
}

Output

Non-palindromic string is: 
sasss
  • Time complexity − O(N), as we use for loop to count a total number of characters equal to the given character.

  • Space complexity − O(1), as we don’t use any extra space.

Approach 2

In this approach, we used the same logic in the first one, but we used the for loop to check whether the string was palindrome. Also, we used the count() method to count the total number of given characters in the string.

Algorithm

  • Step 1 − Use the count() method by passing the string as a first parameter and the given character c as a second parameter to count the number of characters equal to the given character in the string.

  • Step 2 − If the count() method returns a value equal to the string’s length, print ‘-1’.

  • Step 3 − In the isPalindrome() function, initialize the ‘i’ with 0, and ‘j’ with string’s length-1. After that, the user for loops to make iterations and compare the starting and ending characters. Also, return false if any mismatch occurs.

  • Step 4 − Insert the given character at any position, and check whether the string is a non-palindrome. If the resultant string is non-palindrome, we got the answer; Otherwise, change the position of the given character in the string, and check again.

Example

#include <bits/stdc++.h>
using namespace std;
// Function to check if a string is a palindrome
bool isPalindrome(string str) {
   // Start from the leftmost and rightmost corners of str
   for (int i = 0, j = str.length() - 1; i < j; i++, j--){
      // If there is a mismatch, then the string is not palindrome; return false.
      if (str[i] != str[j])
         return false;
   }
   return true;
}
// Function to make a string non-palindrome by adding a character
void makeNonPalindrome(string str, char c){
   //   if all characters are the same as a given character, then the string cannot be made non-palindrome
   if (count(str.begin(), str.end(), c) == str.length()) {
      cout << "-1";
      cout << "We can convert the string into a non-palindromic string by adding a given character at any position.";
      return;
   }
   cout << "Non-palindromic string is: " << endl;
   // append the character at the start, and check if it is a palindrome
   string temp = c + str;
   if (!isPalindrome(temp)){
      cout << temp << endl;
   } else {
      cout << c + str << endl;
   }
}
int main() {
   string str = "nayan";
   char c = 'n';
   makeNonPalindrome(str, c);
   return 0;
}

Output

Non-palindromic string is: 
nnayan
  • Time complexity − O(N)

  • Space complexity − O(1)

Conclusion

We learned two approaches to convert a given string to a non-palindromic by inserting the given character at any position. Both approaches use the same logic, but in the first approach, we written the manual function to count the number of the same characters equal to the given characters, and in the second approach, we used the count() method.

The first approach is better for learning purposes, and the second is better for real-time development.

Updated on: 18-Jul-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements