-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestCommonSubsequence.cpp
More file actions
38 lines (36 loc) · 1 KB
/
Copy pathlongestCommonSubsequence.cpp
File metadata and controls
38 lines (36 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* https://leetcode.com/problems/longest-common-subsequence/description/
* String, Dynamic Programming
* Medium
*/
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int size1 = text1.size();
int size2 = text2.size();
int max = 0;
for (int i = 0; i < size1; i++) {
for (int j = 0; j < size2 - 1; j++) {
int curr = 0;
while (i + curr < size1 && j + curr < size2 && text1[i + curr] == text2[i + curr]) {
curr++;
}
if (curr > max) {
max = curr;
}
}
}
return max;
}
};
int main() {
string text1, text2;
cout << "Input text1 and text2:" << endl;
cin >> text1 >> text2;
Solution solution;
int longestCommonSubsequence = solution.longestCommonSubsequence(text1, text2);
cout << "Longest common subsequence for " << text1 << " and " << text2 << ": " << longestCommonSubsequence << endl;
}