Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    The fewest edits between two words

    HardAbout 40 minFull AccessLearn it: Unique Paths and Longest Common Subsequence: 2D state

    The task

    A spell checker ranks suggestions by how many single-letter edits separate a typed word from a dictionary word.

    Complete EditDistance(string a, string b). Return the fewest edits that turn a into b, where one edit inserts one character, deletes one character or replaces one character with another. Matching is case-sensitive. Neither text is null.

    Signature

    public static int EditDistance(string a, string b)

    Examples

    • "csharp", "sharp" → 1. Delete the c.
    • "abc", "acb" → 2. There is no swap edit: replace b with c, then c with b.

    Limits: Texts of up to 1,000 characters each.

    The tests check: empty texts, deleting or inserting everything, equal texts, one deletion, two insertions, no swap move, letter case, every letter replaced, and two 1,000-letter texts in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // The fewest single-character inserts, deletes and replaces that turn a into b.
          public static int EditDistance(string a, string b)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.