Skip to main content
LeetCode 859, Easy. Topics: Hash Table, String. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:

Problem

Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].
  • For example, swapping at indices 0 and 2 in "abcd" results in "cbad".

Examples

Constraints

  • 1 <= s.length, goal.length <= 2 * 10^4
  • s and goal consist of lowercase letters.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026