Problem 836
Buddy Strings
Easy · 10 pts · String, Hash Table
Problem
Decide whether you can swap exactly two letters at different positions of S so that S becomes equal to GOAL. A swap is required even if S already equals GOAL; in that case it works only if S has some repeated letter, since swapping two equal letters changes nothing. Otherwise S and GOAL must have the same length and differ at exactly two positions i and j, with S[i] = GOAL[j] and S[j] = GOAL[i].
Input
The first line contains S. The second line contains GOAL. Both use lowercase letters.
Output
Print YES if one swap in S can produce GOAL, otherwise NO.
Constraints
1 <= |S|, |GOAL| <= 20000
Example input
ab ba
Example output
YES
Explanation
Swapping the two letters of ab gives ba, which equals GOAL, so the answer is YES. For ab and ab the only swap would give ba, so that case is NO.