Leetcode Problem 161. One Edit Distance

161. One Edit Distance

Leetcode Solutions

One Pass Algorithm for One Edit Distance

  1. Check the length difference between s and t. If it's more than 1, return false.
  2. Ensure s is the shorter string. If not, swap s and t.
  3. Iterate through both strings comparing characters at the same index.
  4. If a difference is found: a. If strings are the same length, check if the substrings from the next index are identical. b. If t is longer, check if the substring of s from the current index is identical to the substring of t from the next index.
  5. If no differences are found, check if t is one character longer than s.
  6. Return true if one edit distance is confirmed, otherwise return false.
UML Thumbnail

Iterative Comparison with Early Termination

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...