- class Solution {
- public:
- int longestCommonSubsequence(string text1, string text2) {
- int l1 = text1.length();
- int l2 = text2.length();
- if (l1 == 0 || l2 == 0) {
- return 0;
- }
- if (l1==1 && l2==1) {
- return text1 == text2;
- }
- if (text1[l1-1] == text2[l2-1]) {
- return 1 + longestCommonSubsequence(text1.substr(0,l1-1), text2.substr(0,l2-1));
- } else {
- return max(longestCommonSubsequence(text1, text2.substr(0,l2-1)),longestCommonSubsequence(text1.substr(0,l1-1), text2));
- }
- }
- };
Raw Paste