1790. Check if One String Swap Can Make Strings Equal
Difficulty: Easy
Topics: Hash Table
, String
, Counting
You are given two strings s1
and s2
of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.
Return true
if it is possible to make both strings equal by performing at most one string swap on e*xactly one* of the strings. Otherwise, return false
.
Example 1:
- Input: s1 = "bank", s2 = "kanb"
- Output: true
- Explanation: For example, swap the first character with the last character of s2 to make "bank".
Example 2:
- Input: s1 = "attack", s2 = "defend"
- Output: false
- Explanation: It is impossible to make them equal with one string swap.
Example 3:
- Input: s1 = "kelb", s2 = "kelb"
- Output: true
- Explanation: The two strings are already equal, so no string swap operation is required.
Constraints:
1 <= s1.length, s2.length <= 100
s1.length == s2.length
-
s1
ands2
consist of only lowercase English letters.
Hint:
- The answer is false if the number of nonequal positions in the strings is not equal to
0
or2
. - Check that these positions have the same set of characters.
Solution:
We need to determine if we can make two given strings equal by performing at most one string swap on exactly one of the strings.
Approach
-
Immediate Check for Equality: If the two strings are already equal, return
true
immediately as no swap is needed. - Collect Differing Indices: Traverse both strings and collect the indices where the characters differ.
-
Check Differing Indices Count: If the number of differing indices is not exactly 2, return
false
because a single swap can only correct two differing positions. - Validate Swap Possibility: Check if swapping the characters at the two differing indices in one of the strings would make them equal. This is verified by ensuring the characters at the differing indices in the first string match the characters at the corresponding indices in the second string when swapped.
Let's implement this solution in PHP: 1790. Check if One String Swap Can Make Strings Equal
<?php
/**
* @param String $s1
* @param String $s2
* @return Boolean
*/
function areAlmostEqual($s1, $s2) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage
$s1 = "bank";
$s2 = "kanb";
var_dump(canSwapToMakeEqual($s1, $s2)); // Output: true
$s1 = "attack";
$s2 = "defend";
var_dump(canSwapToMakeEqual($s1, $s2)); // Output: false
$s1 = "kelb";
$s2 = "kelb";
var_dump(canSwapToMakeEqual($s1, $s2)); // Output: true
?>
Explanation:
-
Immediate Check: If the strings are already identical, we return
true
immediately. -
Collect Differences: We iterate through each character of the strings and collect indices where characters differ. If we collect more than two indices, we return
false
immediately as more than one swap is needed. - Check Indices Count: If exactly two indices differ, we check if swapping the characters at these indices in one of the strings would make them equal. This is done by verifying that the characters at these positions in each string are cross-equal (i.e., the character at the first index in the first string matches the character at the second index in the second string and vice versa).
Complexity:
- Time Complexity: O(n), where n is the length of the strings, as we loop through the strings once.
- Space Complexity: O(1) (excluding the input size), as we use a small array to store at most two indices.
This approach efficiently checks the necessary conditions with a time complexity of O(n), where n is the length of the strings, ensuring optimal performance.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)