Home › Interview Questions › Return true if two strings match with only one swa…

Return true if two strings match with only one swap.

🟡 Medium Coding Junior level
1Times asked
Jun 2026Last seen
Jun 2026First seen

💡 Model Answer

To determine if two strings can be made equal by swapping exactly one pair of characters, first check that the strings have the same length. Then iterate through the strings and record the indices where the characters differ. If there are exactly two mismatched positions i and j, check whether swapping the characters at these positions in the first string makes it equal to the second string. If so, return true; otherwise return false. If the number of mismatches is not exactly two, return false. This algorithm runs in O(n) time and O(1) additional space, where n is the length of the strings.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500