150 najlepszych wywiadówŚredni

Edytuj odległość

Szczegółowy przewodnik i implementacja Python dla problemu „Edytuj odległość”.

Oświadczenie o problemie

Średni

Biorąc pod uwagę dwa ciągi słowo1 i słowo2, zwróć minimalną liczbę operacji wymaganych do konwersji słowa1 na słowo2.

Na słowie dozwolone są trzy następujące operacje:

1. Wstaw znak

2. Usuń znak

3. Zamień znak

Napisz funkcję minDistance(word1: str, word2: str) -> int.

Ograniczenia
  • 0 <= len(word1), len(word2) <= 500
  • word1 and word2 consist of lowercase English letters

Przykłady

Example 1
Input
word1 = "horse", word2 = "ros"
Output
3
Explanation

horse -> rorse (replace 'h' with 'r') -> rose (remove 'r') -> ros (remove 'e').

Example 2
Input
word1 = "intention", word2 = "execution"
Output
5
Explanation

intention -> extention -> exention -> exection -> execution. Total 5 operations.

Need a Hint?
Rozważ użycie struktur danych specyficznych dla 2D DP, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Otwórz w Edytorze
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.