이 튜토리얼에서는 한 문자만 변경하여 문자열을 회문 문자열로 변환하는 프로그램에 대해 설명합니다.
이를 위해 문자열이 제공됩니다. 우리의 임무는 한 문자만 변경하여 주어진 문자열을 회문으로 변환하는 것입니다.
예시
#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