DEV Community

Cover image for 1790. Check if One String Swap Can Make Strings Equal
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on • Edited on

1790. Check if One String Swap Can Make Strings Equal

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 and s2 consist of only lowercase English letters.

Hint:

  1. The answer is false if the number of nonequal positions in the strings is not equal to 0 or 2.
  2. 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

  1. Immediate Check for Equality: If the two strings are already equal, return true immediately as no swap is needed.
  2. Collect Differing Indices: Traverse both strings and collect the indices where the characters differ.
  3. 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.
  4. 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
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Immediate Check: If the strings are already identical, we return true immediately.
  2. 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.
  3. 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:

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️