ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมแปลงสตริงเป็นสตริงพาลินโดรมโดยการเปลี่ยนอักขระเพียงตัวเดียว
สำหรับสิ่งนี้เราจะได้รับสตริง งานของเราคือการแปลงสตริงที่กำหนดให้เป็นพาลินโดรมโดยเปลี่ยนอักขระเพียงตัวเดียว
ตัวอย่าง
#include<bits/stdc++.h>
using namespace std;
//checking if conversion to palindrome
//is possible
bool if_palindrome(string str){
int n = str.length();
//counting number of characters
//to be changed
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 = "abccaa";
if (if_palindrome(str))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
} ผลลัพธ์
Yes