Skip to content

Instantly share code, notes, and snippets.

@bexp
Created November 6, 2017 20:49
Show Gist options
  • Save bexp/43a2f81a85821805814847600a648543 to your computer and use it in GitHub Desktop.
Save bexp/43a2f81a85821805814847600a648543 to your computer and use it in GitHub Desktop.
int longest_substring(const string& str1, const string& str2) {
int n = str1.length();
int m = str2.length();
int suffix[n + 1][m + 1] = {};
int result = 0;
for (int i = 0; i <= n;i++) {
for (int j = 0; j <=m; j++) {
if (i == 0 || j == 0) {
suffix[i][j] = 0;
} else if (str1.at(i - 1) == str2.at(j - 1)) {
suffix[i][j] = suffix[i - 1][j - 1] + 1;
result = max(result, suffix[i][j]);
} else {
suffix[i][j] = 0;
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment