Prepin
Log in

Distance of Edits

Approach / Explanation

The concept of "Edit Distance" quantifies the similarity between two strings, known as the source and target. It indicates the smallest number of edit operations (insertions, deletions, substitutions) required to convert the source string into the target string. This concept originates from computational linguistics and Natural Language Processing (NLP), but it also represents a fundamental dynamic programming challenge in the realm of algorithms. To address this issue, it's essential to have a solid grasp of the dynamic programming technique and understand its application in solving such problems. You should explain how the Edit Distance problem can be approached using this method and potentially create an algorithm or function to illustrate this. The crucial points to keep in mind when tackling this problem are: If the last characters of the two strings match, the cost is 0; if they differ, the cost is 1. It is necessary to evaluate all potential combinations of substrings and determine the minimum cost at each stage. Utilize dynamic programming by storing and reusing results that have already been calculated in a table.

Suggested Answer

For instance, when the source string is kitten and the target string is sitting , the edit distance amounts to 3: Replace k with s in kitten , Replace e with i in kitten , Add g to the end of kitten . This can be computed using dynamic programming in Python as shown below: def edit_distance ( s : str , t : str ) - > int : rows = len ( s ) + 1 cols = len ( t ) + 1 dist = [ [ 0 for x in range ( cols ) ] for x in range ( rows ) ] # source prefixes can be transformed into empty string by dropping all characters for i in range ( 1 , rows ) : dist [ i ] [ 0 ] = i # target prefixes can be transformed into empty string by inserting every character for i in range ( 1 , cols ) : dist [ 0 ] [ i ] = i for col in range ( 1 , cols ) : for row in range ( 1 , rows ) : if s [ row - 1 ] == t [ col - 1 ] : cost = 0 else : cost = 1 dist [ row ] [ col ] = min ( dist [ row - 1 ] [ col ] + 1 , # deletion dist [ row ] [ col - 1 ] + 1 , # insertion dist [ row - 1 ] [ col - 1 ] + cost ) # substitution return dist [ row ] [ col ] print ( edit_distance ( "kitten" , "sitting" ) )

Alternative Answer

Let's examine another case: the source string is intention and the target string is execution . The edit distance between them is 5: Change i to e in intention , Change n to x in intention , Change t to c in intention , Change e to u in intention , Add n at the end of intention . Once more, we will apply a dynamic programming approach to tackle this issue, and the Python code will remain unchanged. print ( edit_distance ( "intention" , "execution" ) ) Therefore, grasping the edit distance concept and how to solve it using dynamic programming can be extremely advantageous. This knowledge can be applied in spell checking, DNA sequencing, and numerous other fields where assessing the similarity between two strings or sequences is crucial.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Google

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.