File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
longest-common-subsequence Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } text1
3+ * @param {string } text2
4+ * @return {number }
5+ */
6+ var longestCommonSubsequence = function ( text1 , text2 ) {
7+ const resultMap = new Map ( ) ;
8+
9+ const dfs = ( i , j ) => {
10+ const mapKey = `${ i } -${ j } ` ;
11+
12+ if ( resultMap . has ( mapKey ) ) {
13+ return resultMap . get ( mapKey ) ;
14+ }
15+
16+ if ( i === text1 . length || j === text2 . length ) {
17+ return 0 ;
18+ }
19+
20+ if ( text1 [ i ] === text2 [ j ] ) {
21+ const resultHit = 1 + dfs ( i + 1 , j + 1 ) ;
22+ resultMap . set ( mapKey , resultHit ) ;
23+ return resultHit ;
24+ }
25+
26+ const resultMiss = Math . max ( dfs ( i + 1 , j ) , dfs ( i , j + 1 ) ) ;
27+ resultMap . set ( mapKey , resultMiss ) ;
28+ return resultMiss ;
29+ }
30+
31+ return dfs ( 0 , 0 ) ;
32+ } ;
33+
34+ // ์๊ฐ๋ณต์ก๋ O(m * n) -> ์ฌ๊ท๋ฅผ ํตํด m๊ณผ n์ด 1์ฉ ์ฆ๊ฐํ๋ฉด์ ์๋ํธ์ m or n ๋ฒ ์งธ ์ธ๋ฑ์ค ๋ฌธ์์ด์ ๋น๊ตํ๋ฏ๋ก??? (์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋ฆฌ๋ทฐ์ด๋ถ์ด ์ฌ์ ๋์๋ฉด ์ค๋ช
๋ถํ๋๋ฆฝ๋๋ค ใ
ใ
)
35+ // ๊ณต๊ฐ๋ณต์ก๋ O(m * n) -> ๋งต์ ํค๊ฐ m * n๊ฐ๋ก ์ต๋๋ก ์์๊ฐ ๋ค์ด์ด
You canโt perform that action at this time.
0 commit comments