상위 150개 인터뷰중간

거리 편집

'거리 편집' 문제에 대한 자세한 가이드 및 Python 구현.

문제 설명

중간

두 개의 문자열 word1과 word2가 주어지면 word1을 word2로 변환하는 데 필요한 최소 연산 수를 반환합니다.

단어에 대해 다음 세 가지 작업이 허용됩니다.

1. 문자를 삽입하세요

2. 캐릭터 삭제

3. 캐릭터 교체

minDistance(word1: str, word2: str) -> int 함수를 작성하세요.

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

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?
세트나 힙과 같은 2D DP 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

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

편집기에서 열기
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

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.