Convert the string into palindrome string by changing only one character - GeeksforGeeks

09 Apr, 2021
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 implementation of above idea :  
C++
Java
Python3
C#
PHP
Javascript
// 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 palindromebool 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;}
// 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
# 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 palindromedef 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 Codestr = "abccaa"if (checkPalindrome(str)) :    print ("Yes")else :    print ("No")     # This code is contributed by# Manish Shaw(manishshaw1)
// 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
<?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 palindromefunction 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.?>
<script>    // Javascript 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){         let n = str.length;                // Counting number of characters        // that should be changed.        let count = 0;        for (let i = 0; i < parseInt(n/2, 10); ++i)            if (str[i] != str[n - i - 1])                ++count;         // If count of changes is less than        // or equal to 1        return (count <= 1);    }         let str = "abccaa";       if (checkPalindrome(str))       document.write("Yes");    else       document.write("No");     // This code is contributed by divyesh072019.</script>
Output:  
Yes
 
YouTubeGeeksforGeeks502K subscribersConvert the string into palindrome string by changing only one character | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:41•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=0h7eK2NUKow" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
nitin mittal
manishshaw1
Akanksha_Rai
divyesh072019
palindrome
Strings
Strings
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Convert string to char array in C++
Array of Strings in C++ (5 Different Ways to Create)
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Reverse words in a given string
Top 50 String Coding Problems for Interviews