Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题

B1. Character Swap (Easy Version)

This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.

After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.

Ujan has two distinct strings 𝑠 and 𝑡 of length 𝑛 consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions 𝑖 and 𝑗 (1≤𝑖,𝑗≤𝑛, the values 𝑖 and 𝑗 can be equal or different), and swaps the characters 𝑠𝑖 and 𝑡𝑗. Can he succeed?

Note that he has to perform this operation exactly once. He has to perform this operation.

Input

The first line contains a single integer 𝑘 (1≤𝑘≤10), the number of test cases.

For each of the test cases, the first line contains a single integer 𝑛 (2≤𝑛≤104), the length of the strings 𝑠 and 𝑡.

Each of the next two lines contains the strings 𝑠 and 𝑡, each having length exactly 𝑛. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

Output

For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise.

You can print each letter in any case (upper or lower).

Example

input

4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca

output

Yes
No
No
No

Note

In the first test case, Ujan can swap characters 𝑠1 and 𝑡4, obtaining the word "house".

In the second test case, it is not possible to make the strings equal using exactly one swap of 𝑠𝑖 and 𝑡𝑗.

题意

现在给你两个字符串s和t,然后你可以选择i,j两个位置,交换s[i]和t[j]。问你在只交换一次的情况下,能不能使得两个字符串相同。

题解

我们遍历两个字符串,我们进行对比,看一共有多少个位置不同。

如果所有位置都相同,输出yes

如果只有一个位置不同,那么一定是no。

如果是两个位置不同,我们就暴力枚举交换s[i],t[j]还是s[j]和t[i]。

其他情况都是no

代码

#include<bits/stdc++.h>
using namespace std;
int n;
string s,t;
void solve(){
	cin>>n;
	cin>>s>>t;
	vector<int>pos;
	for(int i=0;i<s.size();i++){
		if(s[i]!=t[i]){
			pos.push_back(i);
		}
	}
	if(pos.size()==0){
		puts("YES");
	}else if(pos.size()==1){
		puts("NO");
	}else if(pos.size()==2){
		swap(s[pos[0]],t[pos[1]]);
		if(s==t){
			puts("YES");
			return;
		}
		swap(s[pos[0]],t[pos[1]]);
		swap(t[pos[0]],s[pos[1]]);
		if(s==t){
			puts("YES");
			return;
		}
		puts("NO");
	}else{
		puts("NO");
	}
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--)
		solve();
}
posted @ 2019-11-07 16:37  qscqesze  阅读(449)  评论(0编辑  收藏  举报