Buddy Strings - Complete Solution Guide
Buddy Strings is LeetCode problem 859, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given two strings s and goal , return true if you can swap two letters in s so the result is equal to goal , otherwise, return false . Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j] . For example, swapping at indices 0 and 2 in "abcd" results in "cbad" . Example 1: Input: s = "ab", goal = "ba" Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal. Example 2: Input:
Detailed Explanation
The problem asks us to determine if two strings, `s` and `goal`, can become equal if we swap exactly two characters within string `s`. We must return `true` if such a swap is possible, and `false` otherwise. The constraints are that the strings contain only lowercase letters and have a maximum length of 2 * 10^4.
Solution Approach
The solution first checks if the lengths of the strings are different. If they are, it immediately returns `false`. If the strings are equal, it checks if string `s` contains duplicate characters. If so, it returns `true` (because swapping the duplicate characters will result in `s` itself). If the strings are not equal, it finds the indices where `s` and `goal` differ. It checks if there are exactly two such indices. If there are, it swaps the characters at those indices in `s` conceptually, and then returns `true` if the swapped string is equal to the `goal` string, otherwise `false`.
Step-by-Step Algorithm
- Step 1: Check if the lengths of `s` and `goal` are different. If they are, return `false`.
- Step 2: If `s` and `goal` are equal, check for duplicate characters in `s`. Iterate through the characters of `s` and use a `set` or frequency array to determine if any character appears more than once. If a duplicate is found, return `true`. If no duplicates are found after iterating through all characters, return `false`.
- Step 3: If `s` and `goal` are not equal, find the indices where they differ. Store these indices in a list or array.
- Step 4: Check the number of differing indices. If the number of differing indices is not equal to 2, return `false`.
- Step 5: If there are exactly two differing indices, `i` and `j`, check if `s[i] == goal[j]` and `s[j] == goal[i]`. If this condition is true, return `true`; otherwise, return `false`.
Key Insights
- Insight 1: The lengths of the two strings must be equal for a swap to potentially make them identical.
- Insight 2: If the strings are equal, we need to check if there are duplicate characters in `s`. If there is at least one duplicate, swapping those identical characters will result in the same string.
- Insight 3: If the strings are not equal, we need to find the indices where the characters differ. There should be exactly two such indices. If there are more than two differences, no single swap can fix it. Finally, we must verify that swapping the characters at these two indices in `s` results in `goal`.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, String.
Companies
Asked at: DoorDash.