Codeforces Round #598 (Div. 3)F - Equalizing Two Strings

F. Equalizing Two Strings

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two strings ss and tt both of length nn and both consisting of lowercase Latin letters.

In one move, you can choose any length lenlen from 11 to nn and perform the following operation:

  • Choose any contiguous substring of the string ss of length lenlen and reverse it;
  • at the same time choose any contiguous substring of the string tt of length lenlen and reverse it as well.

Note that during one move you reverse exactly one substring of the string ss and exactly one substring of the string tt.

Also note that borders of substrings you reverse in ss and in tt can be different, the only restriction is that you reverse the substrings of equal length. For example, if len=3len=3 and n=5n=5, you can reverse s[1…3]s[1…3] and t[3…5]t[3…5], s[2…4]s[2…4] and t[2…4]t[2…4], but not s[1…3]s[1…3] and t[1…2]t[1…2].

Your task is to say if it is possible to make strings ss and tt equal after some (possibly, empty) sequence of moves.

You have to answer qq independent test cases.

Input

The first line of the input contains one integer qq (1≤q≤1041≤q≤104) — the number of test cases. Then qq test cases follow.

The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of ss and tt.

The second line of the test case contains one string ss consisting of nn lowercase Latin letters.

The third line of the test case contains one string tt consisting of nn lowercase Latin letters.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each test case, print the answer on it — "YES" (without quotes) if it is possible to make strings ss and tt equal after some (possibly, empty) sequence of moves and "NO" otherwise.

Example

input

Copy

4
4
abcd
abdc
5
ababa
baaba
4
asdf
asdg
4
abcd
badc

output

Copy

NO
YES
NO
YES

 

 

分析:首先yes的条件要两个子串的组成字母不同类型的个数完全一样,否则no。完全一样后如果有某个字母出现超过两次,那么先把这两个相同的字母移到一起,然后只需要每次都选择长度为2的操作,并且其中一个串只选择翻转这两个相同的字母,那么就可以做到改变其中一个串并且保持另一个串不变,很容易可以得到答案。若,没有两个相同的字母,那么一定是两两不同的,那么两个串的每个字母一定是一一对应的。那么固定一个串每次交换最后两位,让另一个串从第一位开始匹配,若匹配完前n-2项后后两项相同,那么yes,否则no。

#include <bits/stdc++.h>

using namespace std;
char s[200004], t[200004];
char cs[26], ct[26];

int main() {
    int q;
    cin >> q;
    while (q--) {
        int n;
        cin >> n;
        scanf("%s%s", s, t);
        memset(cs, 0, sizeof(cs));
        memset(ct, 0, sizeof(ct));
        for (int i = 0; i < n; ++i) {
            cs[s[i] - 'a']++;
            ct[t[i] - 'a']++;
        }
        bool yes = 1;
        for (int i = 0; i < 26; ++i) {
            if (cs[i] != ct[i])yes = 0;
        }
        if (!yes) {
            puts("NO");
            continue;
        }
        yes = 0;
        for (int i = 0; i < 26; ++i) {
            if (cs[i] >= 2)yes = 1;
        }
        if (yes) {
            puts("YES");
            continue;
        }
        for (int i = 0; i < n - 2; ++i) {
            int pos = -1;
            for (int j = 0; j < n; ++j) {
                if (t[j] == s[i]) {
                    pos = j;
                    break;
                }
            }
            while (pos > i) {
                swap(s[n - 1], s[n - 2]);
                swap(t[pos], t[pos - 1]);
                pos--;
            }
        }
        if (s[n - 1] == t[n - 1])puts("YES");
        else puts("NO");
    }

}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值