Two accounting clerks each transcribed the same transaction code by hand into their own ledger, producing strings s1 and s2 of equal length made only of lowercase English letters. Before the books are reconciled, an auditor is allowed to correct at most one of the two ledgers by swapping the characters written at two positions within that single ledger's code (the auditor may also swap a position with itself, which changes nothing and effectively means no correction is made). The auditor may only touch one of the two ledgers — never a mix of characters from both. Given s1 and s2, determine whether the auditor can make the two codes identical.
Line 1: the string s1. Line 2: the string s2, of the same length as s1.
Print "YES" if a single swap (or an effective no-op swap of a position with itself) applied to exactly one of the two strings can make them equal, otherwise print "NO".
1 <= length of s1 (equal to length of s2) <= 1000 s1 and s2 consist only of lowercase English letters 'a'-'z'.
Example 1
Input
abcd abdc
Expected
YES
Explanation
The strings differ only at positions 2 and 3 (0-indexed): s1 has 'c','d' there while s2 has 'd','c'. Swapping s1's characters at positions 2 and 3 turns "abcd" into "abdc", which equals s2, so the answer is YES.
Example 2
Input
abcd abce
Expected
NO
Explanation
The strings differ only at position 3 ('d' vs 'e'), a single mismatched position. A swap moves two characters within one string and can only fix a mismatch by pairing it with a second mismatch elsewhere; with just one mismatch that is impossible, so the answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →