DEV Community

Wenqi Jiang
Wenqi Jiang

Posted on

Longest Common Substring

https://binarysearch.com/problems/Longest-Common-Substring

int solve(string s0, string s1) {
    int m = s0.size(), n = s1.size(), res = 0;
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (s0[i] == s1[j]) dp[i + 1][j + 1] = dp[i][j] + 1;
            res = max(res, dp[i + 1][j + 1]);
        }
    }
    return res;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)