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 s and t of length n 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 i and j (1≤i,j≤n, the values i and j can be equal or different), and swaps the characters si and tj. 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 k (1≤k≤10), the number of test cases.

For each of the test cases, the first line contains a single integer n (2≤n≤104), the length of the strings s and t.

Each of the next two lines contains the strings s and t, each having length exactly n. 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
inputCopy
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca
outputCopy
Yes
No
No
No
Note
In the first test case, Ujan can swap characters s1 and t4, obtaining the word “house”.

In the second test case, it is not possible to make the strings equal using exactly one swap of si and tj.

//最多只能交换一次。肯定是只有两组不同的然后交换得到正解。
假设a和b串。其实第二次不同的字符就是第一次那两个。所以我们只要记住a串出现一个字符且出现2次,b串出现一个字符且出现2次,即可

#include<bits/stdc++.h>
using namespace std;
string a,b;
int t,n;
int ans[26];
int cnt[26];
int main()
{
  scanf("%d",&t);
  while(t--)
  {
    memset(ans,0,sizeof(ans));
    memset(cnt,0,sizeof(cnt));
    scanf("%d",&n);
    cin>>a>>b;
    int res=0,res1=0,res2=0,res3=0;
    for(int i=0;i<n;i++)
    {
      if(a[i]!=b[i]) {
        ans[a[i]-'a']++;
        cnt[b[i]-'a']++;
      }
    }
    for(int i=0;i<26;i++) {if(ans[i]==2) res++;if(ans[i]>0) res1++;}
    for(int i=0;i<26;i++) {if(cnt[i]==2) res2++;if(cnt[i]>0) res3++;}
    if(res==1&&res1==1&&res2==1&&res3==1) puts("Yes");
    else puts("No");
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值