Given a string, the task is to check if the string can be made palindrome by swapping a character only once.
[NOTE: only one swap and only one character should be swapped with another character]
Input : bbg Output : true Explanation: Swap b(1st index) with g. Input : bdababd Output : true Explanation: Swap b(0th index) with d(last index) or Swap d(1st index) with b(second index) Input : gcagac Output : false
This algorithm was based on a thorough analysis of the behavior and possibility of the forming string palindrome. By this analysis, I got the following conclusions :
1. Firstly, we will be finding the differences in the string that actually prevents it from being a palindrome.
…..a) To do this, We will start from both the ends and comparing one element from each end at a time, whenever it does match we store the values in a separate array as along with this we keep a count on the number of unmatched items.
2. If the number of unmatched items is more than 2, it is never possible to make it a palindrome string by swapping only one character.
3. If (number of unmatched items = 2) – it is possible to make the string palindrome iff the characters present in first unmatched set are same as the characters present in second unmatched set. (For example : try this out “bdababd”).
4. If (number of unmatched items = 1)
…..a) if (length of string is even) – it is not possible to make a palindrome string out of this.
…..b) if (length of string is odd) – it is possible to make a palindrome string out of this if one of the unmatched character matches with the middle character.
5. If (number of unmatched items = 0) – palindrome is possible if we swap the position of any same characters.
true true false
Time Complexity : O(n)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Lexicographically smaller string by swapping at most one character pair
- Check if two strings can be made equal by swapping one character among each other
- Check for Palindrome after every character replacement Query
- Remove a character from a string to make it a palindrome
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Convert the string into palindrome string by changing only one character.
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Nearest smaller character to a character K from a Sorted Array
- Replace every character of string by character whose ASCII value is K times more than it
- Find a string such that every character is lexicographically greater than its immediate next character
- Longest Common Prefix using Character by Character Matching
- Count substrings that starts with character X and ends with character Y
- Modify the string such that every character gets replaced with the next character in the keyboard
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace all occurrences of character X with character Y in given string
- Shortest distance to every other character from given character
- Replace every character of a string by a different character
- Sorting array with conditional swapping
- Reduce Hamming distance by swapping two characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.