#K9541. Exactly One Swap Operation
Exactly One Swap Operation
Exactly One Swap Operation
You are given two strings S
and T
of equal length. Your task is to determine whether it is possible to make S
equal to T
by performing exactly one swap operation on S
. In one swap operation, you can choose two different positions in S
and swap the characters at those positions.
The challenge is to check if there exists exactly two positions where the strings differ and swapping them in S
will result in T
. Otherwise, the answer is NO
.
More formally, let the two strings be \(S\) and \(T\). You need to check if there exist indices \(i\) and \(j\) with \(i \lt j\) such that if you swap \(S_i\) and \(S_j\), then \(S\) becomes equal to \(T\), and also note that the swap must be performed even if the two strings are initially identical.
For example, if \(S = \texttt{abcd}\) and \(T = \texttt{abdc}\), then by swapping the third and fourth characters of \(S\), you obtain \(T\), and the answer is YES
.
inputFormat
The input consists of two lines:
- The first line contains the original string
S
. - The second line contains the target string
T
.
Both strings contain only printable characters and have the same length.
outputFormat
Output a single line: YES
if it is possible to make S
equal to T
with exactly one swap operation; otherwise, output NO
.
abcd
abdc
YES
</p>