Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- /**
- * @param A, B: Two string.
- * @return: the length of the longest common substring.
- */
- int longestCommonSubstring(string &A, string &B) {
- // write your code here
- if (A.length() > B.length()) {
- return longestCommonSubstring(B, A);
- }
- string str1, str2;
- int maxlen = 0, len = A.length();
- for (int i = 0; i < len; i++) { //1 * len
- for (int j = 1; j <= len - i; j++) { //
- str1 = A.substr(i, j);
- for (int m = 0; m <= B.length() - j; m++) {
- str2 = B.substr(m, j);
- if (str1 == str2) {
- maxlen = max(maxlen, j);
- }
- }
- }
- }
- return maxlen;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement