Longest Common Subsequence

ㅋㅋ·2022년 12월 15일
0

알고리즘-leetcode

목록 보기
70/135

두 문자열에서 가장 긴 subsequence의 길이를 구하는 문제

class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        
        int table[1001][1001]{[0 ... 1000][0 ... 1000] = 0};

        int text1Length = text1.size();
        int text2Length = text2.size();
        for (int i = 1; i <= text1Length; i++)
        {
            for (int j = 1; j <= text2Length; j++)
            {
                if (text1[i - 1] == text2[j - 1])
                {
                    table[i][j] = table[i - 1][j - 1] + 1;
                }
                else
                {
                    table[i][j] = max(table[i - 1][j], table[i][j - 1]);
                }
            }
        }

        return table[text1Length][text2Length];
    }
};

0개의 댓글