Convert the string into palindrome string by changing only one character.
Given a string str. Check if it is possible to convert the string into palindrome string by changing only one character.
Examples:
Input : str = "abccaa" Output : Yes We can change the second last character i.e. 'a' to 'b' to make it palindrome string Input : str = "abbcca" Output : No We can not convert the string into palindrome string by changing only one character.
Approach : The idea is simple, we check character str[i] with str[n-i-1]. If there is a mismatch, we increment count. If count of mismatches exceed 1, we return false. Else we return true.
Below is the the implementation of above idea :
C++
// CPP program to Check if it is // possible to convert the string // into palindrome string by changing // only one character. #include<bits/stdc++.h> using namespace std; // Function to check if it is possible // to convert the string into palindrome bool checkPalindrome(string str){ int n = str.length(); // Counting number of characters // that should be changed. int count = 0; for (int i = 0; i < n/2; ++i) if (str[i] != str[n - i - 1]) ++count; // If count of changes is less than // or equal to 1 return (count <= 1); } // Driver function. int main() { string str = "abccaa"; if (checkPalindrome(str)) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
chevron_right
filter_none
Java
// Java program to Check if it is // possible to convert the string // into palindrome string by changing // only one character. import java.io.*; class GFG { // Function to check if it is possible // to convert the string into palindrome static boolean checkPalindrome(String str){ int n = str.length(); // Counting number of characters // that should be changed. int count = 0; for (int i = 0; i < n/2; ++i) if (str.charAt(i) != str.charAt(n - i - 1)) ++count; // If count of changes is less than // or equal to 1 return (count <= 1); } // Driver Function public static void main(String[] args) { String str = "abccaa"; if (checkPalindrome(str)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by vt_m |
chevron_right
filter_none
Python3
# Python program to Check # if it is possible to # convert the string into # palindrome string by # changing only one character. # Function to check if # it is possible to # convert the string # into palindrome def checkPalindrome(str) : n = len(str) # Counting number of # characters that # should be changed. count = 0 for i in range(0, int(n / 2)) : if (str[i] != str[n - i - 1]) : count = count + 1 # If count of changes # is less than # or equal to 1 if(count <= 1) : return True else : return False # Driver Code str = "abccaa"if (checkPalindrome(str)) : print ("Yes") else : print ("No") # This code is contributed by # Manish Shaw(manishshaw1) |
chevron_right
filter_none
C#
// C# program to Check if it is // possible to convert the string // into palindrome string by changing // only one character. using System; class GFG { // Function to check if it is possible // to convert the string into palindrome static bool checkPalindrome(string str){ int n = str.Length; // Counting number of characters // that should be changed. int count = 0; for (int i = 0; i < n / 2; ++i) if (str[i] != str[n - i - 1]) ++count; // If count of changes is less than // or equal to 1 return (count <= 1); } // Driver Function public static void Main() { string str = "abccaa"; if (checkPalindrome(str)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m |
chevron_right
filter_none
PHP
<?php // PHP program to Check if it is // possible to convert the string // into palindrome string by changing // only one character. // Function to check if it is possible // to convert the string into palindrome function checkPalindrome($str) { $n = strlen($str); // Counting number of characters // that should be changed. $count = 0; for ($i = 0; $i < $n/2; ++$i) if ($str[$i] != $str[$n - $i - 1]) ++$count; // If count of changes // is less than // or equal to 1 return ($count <= 1); } // Driver Code { $str = "abccaa"; if (checkPalindrome($str)) echo "Yes" ; else echo "No" ; return 0; } // This code is contributed by nitin mittal. ?> |
chevron_right
filter_none
Output:
Yes
Recommended Posts:
- Remove a character from a string to make it a palindrome
- Minimum cost to convert string into palindrome
- Convert character array to string in C++
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find the character in first string that is present at minimum index in second string
- Queries to find the first non-repeating character in the sub-string of a string
- Queries to find the last non-repeating character in the sub-string of a given string
- Minimum number of given operations required to convert a string to another string
- Permute a string by changing case
- Convert Hexadecimal value String to ASCII value String
- 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
- Map every character of one string to another such that all occurrences are mapped to the same character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



