[C++] ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Edit Distance Algorithm)

2023. 7. 21. 15:14ยท๐Ÿ“ Computer Science/โœ Algorithm

ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

1. ๊ฐœ๋…

ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Edit Distance Algorithm)์€ ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด A, B๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ๋ฌธ์ž์—ด์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ ์ง€๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ฆ‰, ๋ฌธ์ž์—ด A๊ฐ€ ๋ฌธ์ž์—ด B์™€ ๊ฐ™์•„์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ช‡ ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ์˜ ์—ฐ์‚ฐ์ด๋ž€ ์‚ฝ์ž…(Insertion), ์‚ฝ์ž…(Deletion), ๋Œ€์ฒด(Replacement)๋ฅผ ๋งํ•œ๋‹ค.

 

๋ฌธ์ž์—ด ๊ฐ„์˜ ์œ ์‚ฌ๋„ ์ธก์ •์ฒ˜๋Ÿผ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ๋‘ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์˜ ์œ ์‚ฌ๋„๋ฅผ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํŠนํžˆ ํ”„๋กœ๊ทธ๋žจ์˜ ํ‘œ์ ˆ ์—ฌ๋ถ€, ์ฒ ์ž ์˜ค๋ฅ˜ ๊ฒ€์‚ฌ ๋“ฑ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฒ ์ž ์˜ค๋ฅ˜

 

2. ๊ธฐ๋ณธ ์›๋ฆฌ

 

3. ๋ฌธ์ œ

https://leetcode.com/problems/edit-distance/description/

 

Edit Distance - LeetCode

Can you solve this real interview question? Edit Distance - Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three operations permitted on a word: * Insert a character * D

leetcode.com

#include <string>
#include <algorithm>
using namespace std;

class Solution {
public:
    int dp[502][502]{};

    int minDistance(string word1, string word2) {
        for (int i = 0; i < word1.size(); i++)
            dp[i + 1][0] = i + 1;
        for (int j = 0; j < word2.size(); j++)
            dp[0][j + 1] = j + 1;

        for (int i = 1; i <= word1.size(); i++)
        {
            for (int j = 1; j <= word2.size(); j++)
            {
                if (word1[i - 1] == word2[j - 1])
                    dp[i][j] = dp[i - 1][j - 1];
                else
                    dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
            }
        }
        return dp[word1.size()][word2.size()];
    }
};

 

์ฐธ๊ณ 

https://madplay.github.io/post/levenshtein-distance-edit-distance

 

ํŽธ์ง‘๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ Levenshtein Distance(Edit Distance Algorithm)

๋ฌธ์ž์—ด ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ์•Œ์•„๋‚ด๋Š” ํŽธ์ง‘๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ดํŽด๋ณด์ž.

madplay.github.io

https://velog.io/@rivolt0421/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-Edit-distance

 

๐ŸŽฒ[์•Œ๊ณ ๋ฆฌ์ฆ˜] Edit distance

ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜.

velog.io

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Computer Science/โœ Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] Palindrome
  • [C++] ์ด๋ถ„ ๊ทธ๋ž˜ํ”„
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ
  • [C++] ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Linked List)
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C++] ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Edit Distance Algorithm)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”