PythonStudio
Interview Problems

Edit Distance

medium · dynamic programming

Return the minimum number of single-character insertions, deletions, and replacements needed to transform source into target. Every edit costs 1. Matching characters cost 0; transposition is not one edit. Strings may be empty and have at most 100 characters. Compare Python characters exactly and case-sensitively. Examples (JSON; null means Python None): Input: {"source": "horse", "target": "ros"} Output: 3 Input: {"source": "kitten", "target": "sitting"} Output: 3

Starter code

def edit_distance(source: str, target: str) -> int:
    """Compute unit-cost Levenshtein distance with a rolling row."""
    pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler