Zero to Hero
article thumbnail
Published 2021. 11. 23. 21:43
72. Edit Distance Algorithm
 

Edit Distance - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

정직하게 편집 거리 알고리즘을 구현하는 문제다.

 

1. 편집 거리 알고리즘

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        len1, len2 = len(word1), len(word2)

        board = [[0] * (len2 + 1) for _ in range(len1 + 1)]

        for i in range(len(board)):
            board[i][0] = i
        for i in range(len(board[0])):
            board[0][i] = i

        for i in range(1, len(board)):
            for j in range(1, len(board[0])):
                if word1[i - 1] == word2[j - 1]:
                    board[i][j] = board[i - 1][j - 1]
                else:
                    board[i][j] = min(board[i - 1][j - 1], board[i - 1][j], board[i][j - 1]) + 1

        return board[-1][-1]

 

도전적인 문제를 풀어야하는데...

'Algorithm' 카테고리의 다른 글

51. N-Queens  (0) 2021.11.19
18. 4Sum  (0) 2021.11.16
404. Sum of Left Leaves  (0) 2021.11.11
392. Is Subsequence  (0) 2021.11.09
16. 3Sum Closest  (0) 2021.11.02
profile

Zero to Hero

@Doljae

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!