Palindrome by swapping only one character


In a C++ environment, the palindrome is a process where a set or string remains same from the intial stage of the process to termination of that same particular process. Assume, we have a string denoted as the str[]. And the task is to check the string will be paindrome or not after performing the swapping process with only one character for once.

Here is a general example of the swapping process −

Input : ARBRDD
Output : true
Explanation: Swap the value A(1st index present in the list) with the R.
Input : INDBAN
Output : true
Explanation: Swap the vlaue of I(0th element present in the index set) with
N(last value of the index here) or
Swap I(1st value present in the index list) with N(the second
value present in the index list)
Input : gcagac
Output : the value is totally false

Algorithm to find the palindrome by swaping only one character

In this possible algorithm, we are going to perform the printing process of a palindrome by swapping only one character in a C++ environment. With this algorithm, we will build some C++ syntax to learn about the problem statement in an efficient manner.

  • Step 1 − Start the process.

  • Step 2 − Declare the input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare the input length.

  • Step 5 − Prevent the string from being a palindrome.

  • Step 6 − Count the difference value.

  • Step 7 − Keep the record of those characters which are preventing the process.

  • Step 8 − Start a loop till mid point.

  • Step 9 − Record and store the different characters.

  • Step 10 − Turn on the switch case.

  • Step 11 − If the palindrome value found then return a true.

  • Step 12 − Else return the false as a notation.

  • Step 13 − Find difference in the mid characters.

  • Step 14 − Declare driver code and get the return value.

  • Step 15 − Terminate the process.

Syntax to find the palindrome by swaping only one character

String input = "INPUT STRING";
char[] arr = input.toCharArray();
int swap = 0;
int i = 0;
int j = arr.length-1;
char temp;
while(i<j){
if(arr[i] != arr[j]){
   if(arr[i+1] == arr[j]){
		temp = arr[i];
		arr[i] = arr[i+1];
		arr[i+1] = temp;
		i++;j--;
		swap++;
   } else if(arr[i] == arr[j-1]){
		temp = arr[j];
		arr[j] = arr[j-1];
		arr[j-1] = temp;
		i++;j--;
		swap++;
	} else if(arr[i+1] == arr[j-1] && i+1 != j-1){
		temp = arr[j];
		arr[j] = arr[j-1];
		arr[j-1] = temp;
		temp = arr[i];
		arr[i] = arr[i+1];
		arr[i+1] = temp;
		i++;j--;
		swap = swap+2;
	} else {
		swap = -1;break;
	}
}
else{
   i++;j--;
}

In this possible syntax, we have performed the printing process of a palindrome by swapping only one character in a C++ environment. With these possible syntax, we are heading to build some C++ syntax to learn about the problem statement in a broad manner.

Approaches to Follow

  • Approach 1 − C++ program to extract a palindrome value by swapping only one character with the bool isPalindromePossible(), str.length() and two pointers method

  • Approach 2 − C++ program to extract a palindrome value by swapping only one character by using the Lexiocographical value and iteration method

Approach 1: Using the Bool IsPalindromePossible(), Str.length() and two Pointers Method

Use of Bool IsPalindromePossible() Method

In this possible approach, we are going to apply the boolean isPalindromePossible() method to print a palindrome value by swapping only one character from a particular string.

bool check(string s){
	int n=s.length();
	map<char,int> m;
	for(auto i:s){
		m[i]++;
	}
	int cnt=0;
	for(auto i=m.begin();i!=m.end();i++)
	128{
		if(i->second%2){
			cnt++;
		}
	}
	if(n%2&&cnt==1){return true;}
	if(!(n%2)&&cnt==0){return true;}
	return false;
}

Example

//C++ program palindrome by swapping only one character with the bool isPalindromePossible() method
#include <bits/stdc++.h>
using namespace std;
bool isPalindromePossible(string input){
   int len = input.length();
   int diffCount = 0, i;
   char diff[2][2];
   for (i = 0; i < len / 2; i++){
      if (input[i] != input[len - i - 1]){
         if (diffCount == 2) return false;
         diff[diffCount][0] = input[i];
         diff[diffCount++][1] = input[len - i - 1];
      }
   }
   switch (diffCount){
      case 0:
      return true;
      case 1:{
         char midChar = input[i];
         if (len % 2 != 0 and
         (diff[0][0] == midChar or
         diff[0][1] == midChar))
         return true;
      }
      case 2:
      if ((diff[0][0] == diff[1][0] and
      diff[0][1] == diff[1][1]) or
      (diff[0][0] == diff[1][1] and
      diff[0][1] == diff[1][0]))
      return true;
   }
   return false;
}
int main(){
   cout << boolalpha
   << isPalindromePossible("RDD") << endl;
   cout << boolalpha
   << isPalindromePossible("ARBRDD") << endl;
   cout << boolalpha
   << isPalindromePossible("INDBDKOLDHAKA") << endl;
   return 0;
}

Output

true
false
false

Use of Str.length() Method

In this possible approach, we are going to apply the str.length() method to print a palindrome value by swapping only one character from a particular string.

Example

//C++ program palindrome by swapping only one character with the possible method to convert the string into palindrome string by changing only one character
#include<bits/stdc++.h>
using namespace std;
bool checkPalindrome(string str){
   int n = str.length();
   int count = 0;
   for (int i = 0; i < n/2; ++i)
   if (str[i] != str[n - i - 1])
   ++count;
   return (count <= 1);
}
int main(){
   string str = "ARBRDDINDDBDKOLKATADHAKA";
   if (checkPalindrome(str))
   cout << "Yes - The Process Done " << endl;
   else
   cout << "No - The Process Is Not Done" << endl;
   return 0;
}

Output

No - The Process Is Not Done

Use of two Pointer Approach

In this possible approach, we are going to apply the method by using two pointers to print a palindrome value by swapping only one character from a particular string.

Example

//C++ program palindrome by swapping only one character with the possible method by using two pointer approach
#include <bits/stdc++.h>
using namespace std;
int countSwap(string s){
   int n = s.length();
   int count = 0;
   for (int i = 0; i < n / 2; i++){
      int left = i;
      int right = n - left - 1;
      while (left < right){
         if (s[left] == s[right]){
            break;
         } else{
            right--;
         }
      }
      if (left == right){
         return -1;
      }
      for (int j = right; j < n - left - 1; j++){
         swap(s[j], s[j + 1]);
         count++;
      }
   }
   return count;
}
int main(){
   string s = "ARBRDDKOLDHKAINDBD";
   int ans1 = countSwap(s);
   reverse(s.begin(), s.end());
   int ans2 = countSwap(s);
   cout << max(ans1, ans2);
   return 0;
}

Output

-1

Approach 2: Using the Lexiocographical Value and Iteration Method

Use of the Lexicographical Method

Lexicographical comparison is a comparing function which accepts two arguments for a process. In this possible approach, we are going to apply the lexiocographical method to print a palindrome value by swapping only one character from a particular string.

int main(){
string a;
while(cin>>a){
if(a[0]=='0'){
	break;
}
string s;s=a;
int n=s.length();
int cnt=0;
bool ini=false;
if(n%2){ini=true;}
if(check(s)){
	for(int i=0;i<n/2;i++){
		bool fl=false;
		int j=0;
			for(j=n-1-i;j>i;j--){
			if(s[j]==s[i]){
				fl=true;
				for(int k=j;k<n-1-i;k++){
					swap(s[k],s[k+1]);
					cnt++;
					cout<<cnt<<endl<<flush;
				}
				cout<<" "<<i<<" "<<cnt<<endl<<flush;
				break;
			}
		}
		if(!fl&&ini){
			for(int k=i;k<n/2;k++){
				swap(s[k],s[k+1]);
				cnt++;
			}
			cout<<cnt<<" "<<i<<" "<<endl<<flush;
		}
	}
	cout<<cnt<<endl;
}
else{
cout<<"OUTPUT NOTATION PROCESS"<<endl;

Example

//C++ program check whether is it possible to make string A lexicographically smaller than string B to perform the palindrome swaping with one character
#include <bits/stdc++.h>
using namespace std;
void swap(char& x, char& y){
   char temp = x;
   x = y;
   y = temp;
}
bool IsLexicographicallySmaller(
string A, string B){
   if (A < B){
      return true;
   }
   string temp = A;
   sort(temp.begin(), temp.end());
   int index = -1;
   for (int i = 0; i < A.length(); i++){
      if (A[i] != temp[i]) {
         index = i;
         break;
      }
   }
   if (index == -1){
      return false;
   }
   int j;
   for (int i = 0; i < A.length(); i++){
      if (A[i] == temp[index])
      j = i;
   }
   swap(A[index], A[j]);
   if (A < B){
      return true;
   } else {
      return false;
   }
}
int main(){
   string A = "ABONI DAS";
   string B = "RUDRA BISWAS";
   if (IsLexicographicallySmaller(A, B)) {
      cout << "Yes"
      << "\n";
   } else {
      cout << "No"
      << "\n";
   }
   return 0;
}

Output

Yes

Use of the Iteration Method

In this possible approach, we are going to apply the iteration method to print a palindrome value by swapping only one character from a particular string loop in the range of (0, N/2). Here we are going to perform the palindrome swaping with one character after removal or neglecting character c.

Example

//C++ program to perform the palindrome swaping with one character after removal or neglecting character c and iterate a loop in the range of (0, N/2)
#include <bits/stdc++.h>
using namespace std;
bool check_palindrome(string str, char c){
   int n = str.length(), i = 0, j = n - 1;
   while (i < j) {
      if (str[i] == c)
      i++;
      else if (str[j] == c)
      j--;
      else if (str[i] != str[j])
      return 0;
      else
      i++, j--;
   }
   return 1;
}
string make_palindrome(string str){
   bool is_palindrome = 1;
   int n = str.length();
   if (n == 1 || n == 2) {
      return "YES";
   }
   for (int i = 0; i < n / 2; ++i) {
      if (str[i] != str[n - 1 - i]) {
         is_palindrome
         = check_palindrome(str, str[i])
         || check_palindrome(
         str, str[n - 1 - i]);
         break;
      }
   }
   if (is_palindrome)
   return "Yes";
   else
   return "No";
}
int main(){
   string S = "ARBRDDKOLKATADHAKAINDBANGLADESH";
   string res = make_palindrome(S);
   cout << (res);
   return 0;
}

Output

No

Conclusion

Today in this article we have learned about how to implement the process to construct and apply the various methods to print a palindrome value set by swapping only one character in a C++ environment. With the above mentioned logics, syntax and algoritm; we have tried to build some C++ codes to solve the problem statement in an efficient manner.

Updated on: 27-Dec-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements