Check whether at most one character swap in one string makes two strings equal

Quick Overview

Determine whether two equal-length strings can be made identical by swapping at most two characters in only one of them. The problem tests mismatch counting, reasoning about what a single swap can change, and edge cases such as strings that are already equal.

Check whether at most one character swap in one string makes two strings equal

Company: Epic Games

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 those indices. Return `True` if it is possible to make both strings equal by performing at most one string swap on exactly one of the two strings. Otherwise, return `False`. ### Function Signature ```python def are_almost_equal(s1: str, s2: str) -> bool: ... ``` ### Rules - The two chosen indices may be the same, which leaves the string unchanged. Two strings that are already equal therefore return `True`. - The swap, if any, is applied to only one of the two strings. ### Constraints - `1 <= len(s1), len(s2) <= 100` - `len(s1) == len(s2)` - `s1` and `s2` consist of only lowercase English letters. ### Examples ```text Input: s1 = "bank", s2 = "kanb" Output: True ``` The strings differ only at indices 0 and 3. Swapping the first and last characters of `s2` gives "bank", which equals `s1`. ```text Input: s1 = "attack", s2 = "defend" Output: False ``` The strings differ at all six indices. One swap changes at most two positions, so no single swap on either string can make them equal. ```text Input: s1 = "kelb", s2 = "kelb" Output: True ``` The strings are already equal, so zero swaps are needed, which is within the limit of at most one.

Overview: Determine whether two equal-length strings can be made identical by swapping at most two characters in only one of them. The problem tests mismatch counting, reasoning about what a single swap can change, and edge cases such as strings that are already equal.

Epic Games logo
Epic Games
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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 those indices.

Return True if it is possible to make both strings equal by performing at most one string swap on exactly one of the two strings. Otherwise, return False.

Function Signature

def are_almost_equal(s1: str, s2: str) -> bool:
    ...

Rules

  • The two chosen indices may be the same, which leaves the string unchanged. Two strings that are already equal therefore return True .
  • The swap, if any, is applied to only one of the two strings.

Constraints

  • 1 <= len(s1), len(s2) <= 100
  • len(s1) == len(s2)
  • s1 and s2 consist of only lowercase English letters.

Examples

Input: s1 = "bank", s2 = "kanb"
Output: True

The strings differ only at indices 0 and 3. Swapping the first and last characters of s2 gives "bank", which equals s1.

Input: s1 = "attack", s2 = "defend"
Output: False

The strings differ at all six indices. One swap changes at most two positions, so no single swap on either string can make them equal.

Input: s1 = "kelb", s2 = "kelb"
Output: True

The strings are already equal, so zero swaps are needed, which is within the limit of at most one.