#K51297. One Swap String Conversion
One Swap String Conversion
One Swap String Conversion
Given two strings s and t, determine whether it is possible to convert s into t by performing at most one swap operation on s. In one swap operation, you can choose two distinct indices i and j (with i ≠ j) and exchange s[i]
and s[j]
.
Note that:
- If s is already equal to t, the answer is
YES
. - The lengths of s and t must be equal. Otherwise, the answer is
NO
. - If there are exactly two indices i and j where s and t differ, then the conversion is possible if and only if swapping these two positions in s results in t; mathematically, this can be represented as: $$ s[i] = t[j] \quad \text{and} \quad s[j] = t[i] $$.
Otherwise, the answer is NO
.
inputFormat
The input is read from standard input and has the following format:
T s₁ t₁ s₂ t₂ ... sₜ tₜ
where T
(an integer) is the number of test cases, and for each test case there are two strings s
and t
separated by whitespace.
outputFormat
For each test case, print a single line with YES
if s can be converted into t with at most one swap operation, and NO
otherwise.
1
a a
YES