【蘑菇街】回文串

题目描述

给定一个字符串,问是否能通过添加一个字母将其变为回文串。

输入描述:

一行一个由小写字母构成的字符串,字符串长度小于等于10。

输出描述:

输出答案(YES\NO).

示例1

输入

复制

coco

输出

复制

YES
#include "stdio.h"
#include <math.h>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	string str;
	while (cin >> str)
	{
		int left = 0, right = str.size() - 1;
		int count = 0;
		while (left <= right&&right > -1 && left < str.size())
		{
			if (str[left] == str[right])
			{
				left++;
				right--;
			}
			else
			{
				count++;
				left++;
				if (count > 1)
					break;
			}
		}
		if (count < 2)
			cout << "YES" << endl;
		else
		{
			int left = 0, right = str.size() - 1;
			int count = 0;
			while (left <= right&&right > -1 && left < str.size())
			{
				if (str[left] == str[right])
				{
					left++;
					right--;
				}
				else
				{
					count++;
					right--;
					if (count > 1)
						break;
				}
			}
			if (count < 2)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/leetcodecl/article/details/83016653