Two strings s and t of the same length are given. Determine whether it is possible to make t from s using exactly one reverse of some its substring.
Input
The first line contains the string s, and the second — the string t. Both strings have the same length from 1 to 200000 characters and consist of lowercase Latin letters.
Output
Output «YES», if it is possible to reverse some substring of s to make s equal to t, and «NO», otherwise.
Examples
Input
abcdefg
abedcfg
Output
YES
Input
abcdefg
abdecfg
Output
NO
思路:从前开始找到不一样字母的下标,再从后找到不一样字母的下标。
#include<bits/stdc++.h>
using namespace std;
const int maxx=2e5+10;
char a[maxx],b[maxx];
int main()
{
std::ios::sync_with_stdio(false);
cin>>a>>b;
int len=strlen(a);
int n1=-1,n2=-1;
for(int i=0; i<len; i++)
{
if(a[i]!=b[i])
{
n1=i;
break;
}
}
for(int i=len-1; i>=0; i--)
{
if(a[i]!=b[i])
{
n2=i;
break;
}
}
int flag=1;
for(int i=n1; i<=n2; i++)
{
if(a[i]!=b[n2+n1-i])
{
flag=0;
// cout<<i<<"&&&&"<<j<<endl;
break;
}
}
if(flag==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}