Skip to main content
LeetCode 161, Medium. Topics: Two Pointers, String. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 25 parametrized pytest cases, and a playground notebook:

Problem

Given two strings s and t, return true if they are both one edit distance apart, otherwise return false. A string s is said to be one distance apart from a string t if you can:
  • Insert exactly one character into s to get t.
  • Delete exactly one character from s to get t.
  • Replace exactly one character of s to get t.

Examples

Constraints

  • 0 <= s.length, t.length <= 10^4
  • s and t consist of lowercase letters, uppercase letters, and digits

Solution

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

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026