FastPrepEdit Distance
Problem · String

Edit Distance

Learn this problem
MediumAstrotalk logoAstrotalkFULLTIMENEW GRADPHONE SCREEN

Problem statement

Given two case-sensitive strings source and target, return the minimum number of single-character operations needed to transform source into target.

One operation may insert a character, delete a character, or replace a character. Every operation costs 1.

Function

editDistance(source: String, target: String) → int

Examples

Example 1

source = "horse"target = "ros"return = 3

Replace h with r, delete the second r, and delete e.

Example 2

source = "sea"target = "eat"return = 2

Delete s and insert t at the end.

Example 3

source = ""target = "abc"return = 3

Insert all three target characters.

Constraints

  • 0 <= source.length, target.length <= 6
  • Both strings contain only English letters.
  • Character comparisons are case-sensitive.
drafts saved locally
public int editDistance(String source, String target) {
    // write your code here
}
source"horse"
target"ros"
expected3
checking account