Bird
Raised Fist0
DSA Goprogramming~30 mins

Edit Distance Problem Levenshtein in DSA Go - Build from Scratch

Choose your learning style10 modes available
Edit Distance Problem Levenshtein
📖 Scenario: You are building a simple text comparison tool that helps find how different two words are. This is useful in spell checkers or search suggestions.
🎯 Goal: Build a program that calculates the Levenshtein edit distance between two words. This distance tells how many single-letter changes (insertions, deletions, or substitutions) are needed to change one word into the other.
📋 What You'll Learn
Create two string variables with exact words
Create a 2D slice to store distances
Use nested loops to fill the distance table using Levenshtein rules
Print the final edit distance value
💡 Why This Matters
🌍 Real World
Spell checkers, search engines, and auto-correct tools use edit distance to suggest correct words or find similar words.
💼 Career
Understanding dynamic programming and string algorithms is important for software engineers working on text processing, natural language processing, and search technologies.
Progress0 / 4 steps
1
Create the two words to compare
Create two string variables called word1 and word2 with values "kitten" and "sitting" respectively.
DSA Go
Hint

Use := to create and assign the strings word1 and word2.

2
Create the distance table
Create a 2D slice of integers called dist with size len(word1)+1 rows and len(word2)+1 columns. Initialize the first row with values from 0 to len(word2) and the first column with values from 0 to len(word1).
DSA Go
Hint

Use make to create the 2D slice. Use loops to fill the first row and column with increasing numbers.

3
Fill the distance table using Levenshtein rules
Use nested for loops with variables i from 1 to len(word1) and j from 1 to len(word2). Inside, check if word1[i-1] equals word2[j-1]. If yes, set dist[i][j] = dist[i-1][j-1]. Otherwise, set dist[i][j] to 1 plus the minimum of dist[i-1][j], dist[i][j-1], and dist[i-1][j-1].
DSA Go
Hint

Use nested loops and compare characters at i-1 and j-1. Use a helper function to find the minimum of three numbers.

4
Print the final edit distance
Print the value at dist[len(word1)][len(word2)] using fmt.Println to show the Levenshtein edit distance between word1 and word2.
DSA Go
Hint

Use fmt.Println(dist[len(word1)][len(word2)]) to print the final edit distance.

Practice

(1/5)
1. What does the Edit Distance (Levenshtein distance) between two strings measure?
easy
A. The number of matching characters in the same positions
B. The number of characters in the longer string
C. The minimum number of single-character edits to change one string into the other
D. The length difference between the two strings

Solution

  1. Step 1: Understand the definition of Edit Distance

    Edit Distance counts how many changes (insert, delete, replace) are needed to convert one string to another.
  2. Step 2: Identify what is measured

    It measures the minimum number of single-character edits required.
  3. Final Answer:

    The minimum number of single-character edits to change one string into the other -> Option C
  4. Quick Check:

    Edit Distance = minimum edits [OK]
Hint: Edit Distance counts minimal edits to match strings [OK]
Common Mistakes:
  • Confusing edit distance with string length
  • Thinking it counts only matching characters
  • Assuming it measures length difference only
2. Which of the following Go function signatures correctly represents a function to compute Levenshtein distance between two strings s1 and s2?
easy
A. func Levenshtein(s1, s2 string) string
B. func Levenshtein(s1, s2 string) int
C. func Levenshtein(s1 int, s2 int) string
D. func Levenshtein(s1, s2 int) int

Solution

  1. Step 1: Identify input types for strings

    The function must accept two strings as input parameters.
  2. Step 2: Identify output type

    The output is an integer representing the edit distance.
  3. Final Answer:

    func Levenshtein(s1, s2 string) int -> Option B
  4. Quick Check:

    Input: strings, Output: int [OK]
Hint: Function inputs are strings, output is int distance [OK]
Common Mistakes:
  • Using int instead of string for inputs
  • Returning string instead of int
  • Mixing input types incorrectly
3. What is the output of the following Go code snippet computing Levenshtein distance between "kitten" and "sitting"?
package main
import "fmt"
func main() {
    s1 := "kitten"
    s2 := "sitting"
    dist := Levenshtein(s1, s2)
    fmt.Println(dist)
}

func Levenshtein(a, b string) int {
    m, n := len(a), len(b)
    dp := make([][]int, m+1)
    for i := range dp {
        dp[i] = make([]int, n+1)
    }
    for i := 0; i <= m; i++ {
        dp[i][0] = i
    }
    for j := 0; j <= n; j++ {
        dp[0][j] = j
    }
    for i := 1; i <= m; i++ {
        for j := 1; j <= n; j++ {
            cost := 0
            if a[i-1] != b[j-1] {
                cost = 1
            }
            dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+cost)
        }
    }
    return dp[m][n]
}

func min(x, y, z int) int {
    if x < y {
        if x < z {
            return x
        }
        return z
    }
    if y < z {
        return y
    }
    return z
}
medium
A. 6
B. 4
C. 5
D. 3

Solution

  1. Step 1: Understand the strings and operations

    "kitten" to "sitting" requires substitutions k->s, e->i, plus insert g.
  2. Step 2: Calculate minimum edits

    Minimum edits are 2 substitutions + 1 insertion = 3 edits total.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Levenshtein("kitten", "sitting") = 3 [OK]
Hint: Count substitutions and insertions carefully [OK]
Common Mistakes:
  • Counting only substitutions
  • Ignoring insertions
  • Miscounting character positions
4. Identify the bug in this Go function computing Levenshtein distance:
func Levenshtein(a, b string) int {
    m, n := len(a), len(b)
    dp := make([][]int, m)
    for i := range dp {
        dp[i] = make([]int, n)
    }
    for i := 0; i < m; i++ {
        dp[i][0] = i
    }
    for j := 0; j < n; j++ {
        dp[0][j] = j
    }
    for i := 1; i < m; i++ {
        for j := 1; j < n; j++ {
            cost := 0
            if a[i-1] != b[j-1] {
                cost = 1
            }
            dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+cost)
        }
    }
    return dp[m-1][n-1]
}
medium
A. dp slice sizes should be m+1 and n+1, not m and n
B. The cost calculation should be outside the loops
C. The min function is missing
D. The return statement should be dp[0][0]

Solution

  1. Step 1: Check dp slice initialization

    dp must have size m+1 by n+1 to include empty string cases, but code uses m and n only.
  2. Step 2: Understand impact of wrong sizes

    Using m and n causes index out of range errors and incorrect base cases.
  3. Final Answer:

    dp slice sizes should be m+1 and n+1, not m and n -> Option A
  4. Quick Check:

    dp size = m+1, n+1 [OK]
Hint: dp array must cover empty prefixes with +1 size [OK]
Common Mistakes:
  • Initializing dp with wrong sizes
  • Misplacing cost calculation
  • Returning wrong dp cell
5. Given two strings s1 = "intention" and s2 = "execution", what is the minimum edit distance using Levenshtein algorithm? Consider insertions, deletions, and substitutions only.
hard
A. 6
B. 5
C. 8
D. 7

Solution

  1. Step 1: Analyze differences between "intention" and "execution"

    Compare characters and count edits: substitutions and insertions/deletions as needed.
  2. Step 2: Calculate minimal edits

    Minimum edits calculated are 6 total.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Levenshtein("intention", "execution") = 6 [OK]
Hint: Count substitutions plus insertions/deletions carefully [OK]
Common Mistakes:
  • Underestimating substitutions
  • Ignoring insertions/deletions
  • Confusing character positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard