Skip to content
Discussion options

You must be logged in to vote

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 …

Replies: 1 comment 2 replies

Comment options

mah-shamim
Feb 5, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Feb 5, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants