#C3427. Swap to Equalize
Swap to Equalize
Swap to Equalize
Given two strings \( s_1 \) and \( s_2 \) of equal length, determine whether \( s_1 \) can be transformed into \( s_2 \) by performing at most one swap operation on \( s_1 \). A single swap operation consists of choosing two distinct indices \( i \) and \( j \) (where \( i \neq j \)) and swapping the characters at these positions. Formally, you need to check if either:
- \( s_1 = s_2 \), or
- There exist indices \( i \) and \( j \) such that if you swap \( s_1[i] \) and \( s_1[j] \), the resulting string equals \( s_2 \).
Note that if the strings differ in any number of positions other than exactly 2, then it is not possible to obtain \( s_2 \) from \( s_1 \) with a single swap.
It is guaranteed that the two input strings have the same length.
inputFormat
The input consists of two lines:
- The first line contains the string \( s_1 \).
- The second line contains the string \( s_2 \).
Both strings have the same length.
outputFormat
Output a single line containing True
if \( s_1 \) can be transformed into \( s_2 \) using at most one swap of two characters in \( s_1 \); otherwise, output False
.
ab
ba
True