How to solve LeetCode problem 859: Buddy Strings efficiently?