|
1 |
| -varlongestCommonSubsequence=function(text1,text2){ |
2 |
| -letm=text1.length, |
3 |
| -n=text2.length, |
4 |
| -DP=newArray(m+1).fill(0).map((_)=>newArray(n+1).fill(0)); |
5 |
| - |
6 |
| -for(letx=m-1;x>=0;x--) |
7 |
| -for(lety=n-1;y>=0;y--){ |
8 |
| -if(text1[x]===text2[y]){ |
9 |
| -DP[x][y]=1+DP[x+1][y+1]; |
10 |
| -}else{ |
11 |
| -DP[x][y]=Math.max(DP[x+1][y],DP[x][y+1]); |
12 |
| -} |
| 1 | +/** |
| 2 | + * DP - Top Down |
| 3 | + * Matrix - Memoization |
| 4 | + * Time O(N * (M^2)) | Space O(N * M) |
| 5 | + * https://leetcode.com/problems/longest-common-subsequence/ |
| 6 | + *@param {string} text1 |
| 7 | + *@param {string} text2 |
| 8 | + *@return {number} |
| 9 | + */ |
| 10 | +varlongestCommonSubsequence=(text1,text2,p1=0,p2=0,memo=initMemo(text1,text2))=>{ |
| 11 | +constisBaseCase=((p1===text1.length)||(p2===text2.length)); |
| 12 | +if(isBaseCase)return0; |
| 13 | + |
| 14 | +consthasSeen=(memo[p1][p2]!==null); |
| 15 | +if(hasSeen)returnmemo[p1][p2]; |
| 16 | + |
| 17 | +returndfs(text1,text2,p1,p2,memo);/* Time O((N * M) * M)) | Space O((N * M) + HEIGHT) */ |
| 18 | +} |
| 19 | + |
| 20 | +varinitMemo=(text1,text2)=>newArray((text1.length+1)).fill()/* Time O(N) | Space O(N) */ |
| 21 | +.map(()=>newArray((text2.length+1)).fill(null));/* Time O(M) | Space O(M) */ |
| 22 | + |
| 23 | +vardfs=(text1,text2,p1,p2,memo)=>{ |
| 24 | +constleft=longestCommonSubsequence(text1,text2,(p1+1),p2,memo);/* Time O(N * M) | Space O(HEIGHT) */ |
| 25 | + |
| 26 | +constindex=text2.indexOf(text1[p1],p2);/* Time O(M) */ |
| 27 | +constisPrefix=(index!==-1); |
| 28 | + |
| 29 | +constright=isPrefix |
| 30 | + ?(longestCommonSubsequence(text1,text2,(p1+1),(index+1),memo)+1)/* Time O(N * M) | Space O(HEIGHT) */ |
| 31 | + :0; |
| 32 | + |
| 33 | +memo[p1][p2]=Math.max(left,right);/* | Space O(N * M) */ |
| 34 | +returnmemo[p1][p2]; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * DP - Top Down |
| 39 | + * Matrix - Memoization |
| 40 | + * Time O(N * M) | Space O(N * M) |
| 41 | + * https://leetcode.com/problems/longest-common-subsequence/ |
| 42 | + *@param {string} text1 |
| 43 | + *@param {string} text2 |
| 44 | + *@return {number} |
| 45 | + */ |
| 46 | +varlongestCommonSubsequence=(text1,text2,p1=0,p2=0,memo=initMemo(text1,text2))=>{ |
| 47 | +constisBaseCase=((p1===text1.length)||(p2===text2.length)); |
| 48 | +if(isBaseCase)return0; |
| 49 | + |
| 50 | +consthasSeen=(memo[p1][p2]!==null); |
| 51 | +if(hasSeen)returnmemo[p1][p2]; |
| 52 | + |
| 53 | +returndfs(text1,text2,p1,p2,memo);/* Time O(N * M) | Space O((N * M) + HEIGHT) */ |
| 54 | +} |
| 55 | + |
| 56 | +varinitMemo=(text1,text2)=>newArray((text1.length+1)).fill()/* Time O(N) | Space O(N) */ |
| 57 | +.map(()=>newArray((text2.length+1)).fill(null));/* Time O(M) | Space O(M) */ |
| 58 | + |
| 59 | +vardfs=(text1,text2,p1,p2,memo)=>{ |
| 60 | +constleft=(longestCommonSubsequence(text1,text2,(p1+1),(p2+1),memo)+1);/* Time O(N * M) | Space O(HEIGHT) */ |
| 61 | +constright=/* Time O(N * M) | Space O(HEIGHT) */ |
| 62 | +Math.max(longestCommonSubsequence(text1,text2,p1,(p2+1),memo),longestCommonSubsequence(text1,text2,(p1+1),p2,memo)); |
| 63 | + |
| 64 | +constisEqual=(text1[p1]==text2[p2]); |
| 65 | +constcount=isEqual |
| 66 | + ?left |
| 67 | + :right |
| 68 | + |
| 69 | +memo[p1][p2]=count;/* | Space O(N * M) */ |
| 70 | +returnmemo[p1][p2]; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * DP - Bottom Up |
| 75 | + * Matrix - Tabulation |
| 76 | + * Time O(N * M) | Space O(N * M) |
| 77 | + * https://leetcode.com/problems/longest-common-subsequence/ |
| 78 | + *@param {string} text1 |
| 79 | + *@param {string} text2 |
| 80 | + *@return {number} |
| 81 | + */ |
| 82 | +varlongestCommonSubsequence=(text1,text2)=>{ |
| 83 | +consttabu=initTabu(text1,text2);/* Time O(N * M) | Space O(N * M) */ |
| 84 | + |
| 85 | +search(text1,text2,tabu);/* Time O(N * M) | Space O(N * M) */ |
| 86 | + |
| 87 | +returntabu[0][0]; |
| 88 | +}; |
| 89 | + |
| 90 | +varinitTabu=(text1,text2)=> |
| 91 | +newArray((text1.length+1)).fill()/* Time O(N) | Space O(N) */ |
| 92 | +.map(()=>newArray((text2.length+1)).fill(0));/* Time O(M) | Space O(M) */ |
| 93 | + |
| 94 | +varsearch=(text1,text2,tabu)=>{ |
| 95 | +const[n,m]=[text1.length,text2.length]; |
| 96 | + |
| 97 | +for(letx=(n-1);(0<=x);x--){/* Time O(N) */ |
| 98 | +for(lety=(m-1);(0<=y);y--){/* Time O(M) */ |
| 99 | +tabu[x][y]=(text1[x]===text2[y])/* Space O(N * M) */ |
| 100 | + ?(tabu[x+1][y+1]+1) |
| 101 | + :Math.max(tabu[x+1][y],tabu[x][y+1]); |
13 | 102 | }
|
| 103 | +} |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * DP - Bottom Up |
| 108 | + * Matrix - Tabulation |
| 109 | + * Time O(N * M) | Space O(M) |
| 110 | + * https://leetcode.com/problems/longest-common-subsequence/ |
| 111 | + *@param {string} text1 |
| 112 | + *@param {string} text2 |
| 113 | + *@return {number} |
| 114 | + */ |
| 115 | +varlongestCommonSubsequence=(text1,text2)=>{ |
| 116 | +constcanSwap=(text2.length<text1.length); |
| 117 | +if(canSwap)[text1,text2]=[text2,text1]; |
| 118 | + |
| 119 | +lettabu=initTabu(text1);/* Time O(M) | Space O(M) */ |
| 120 | + |
| 121 | +tabu=search(text1,text2,tabu);/* Time O(N * M) | Space O(M) */ |
14 | 122 |
|
15 |
| -returnDP[0][0]; |
| 123 | +returntabu[0]; |
16 | 124 | };
|
| 125 | + |
| 126 | +varinitTabu=(text1)=>newArray((text1.length+1)).fill(0) |
| 127 | + |
| 128 | +varsearch=(text1,text2,tabu)=>{ |
| 129 | +for(letcol=(text2.length-1);(0<=col);col--){/* Time O(N) */ |
| 130 | +consttemp=initTabu(text1);/* Space O(M) */ |
| 131 | + |
| 132 | +for(letrow=(text1.length-1);(0<=row);row--){/* Time O(M) */ |
| 133 | +constisEqual=(text1[row]==text2[col]); |
| 134 | + |
| 135 | +temp[row]=isEqual/* Space O(M) */ |
| 136 | + ?(tabu[(row+1)]+1) |
| 137 | + :Math.max(tabu[row],temp[(row+1)]); |
| 138 | +} |
| 139 | + |
| 140 | +tabu=temp;/* Space O(M) */ |
| 141 | +} |
| 142 | + |
| 143 | +returntabu; |
| 144 | +} |