@@ -20,36 +20,91 @@ public class Geegong {
2020 * @return
2121 */
2222 public int longestCommonSubsequence (String text1 , String text2 ) {
23- char [] chText1 = text1 .toCharArray ();
24- char [] chText2 = text2 .toCharArray ();
25-
26- // text1๋ง ๊ฐ ์์ ๋ณ๋ก char๊ฐ ๊ฐ์ง๋ index๋ฅผ array ๋ก ๊ฐ๋ ๋ฐฐ์ด์ ์์ฑ
27- List <Integer >[] positionIndices = new List [26 ];
28- for (int index = 0 ; index <chText1 .length ; index ++) {
29- if (positionIndices [chText1 [index ] - 'a' ] == null ) {
30- positionIndices [chText1 [index ] - 'a' ] = new ArrayList ();
31- positionIndices [chText1 [index ] - 'a' ].add (index );
32- } else {
33- positionIndices [chText1 [index ] - 'a' ].add (index );
34- }
23+
24+ /**
25+ * DP ๋ฅผ ์ด์ฉํ LIS ๊ตฌํ๊ธฐ
26+ * 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด text1, text2 ๋ค์ ํ๋์ฉ ํ์ด๊ฐ๋ฉฐ
27+ * ์ด์ ์ ์ผ์นํ๋ ๊ฐ character ๋ณ๋ก ํ์๋ค์ ๊ธฐ๋กํด๊ฐ๋ฉฐ ๋ LIS ๋ก ๋ถ์ผ ์ ์๋์ง ์ ๋ฆฌํด๊ฐ๋ ๊ธฐ๋ฒ
28+ * ์ฐธ๊ณ ๋ธ๋ก๊ทธ : https://velog.io/@emplam27/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EA%B7%B8%EB%A6%BC%EC%9C%BC%EB%A1%9C-%EC%95%8C%EC%95%84%EB%B3%B4%EB%8A%94-LCS-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-Longest-Common-Substring%EC%99%80-Longest-Common-Subsequence
29+ * time complexity : O(M * N) => O (N)
30+ * space complexity : O(M * N) => O (N)
31+ */
32+ char [] ch1 = text1 .toCharArray ();
33+ char [] ch2 = text2 .toCharArray ();
34+
35+ int [][] LISArr = new int [ch1 .length + 1 ][ch2 .length + 1 ];
36+
37+ // first, fill zeros in first row, first column for convenient
38+ for (int idx =0 ; idx <=ch2 .length ; idx ++) {
39+ LISArr [0 ][idx ] = 0 ;
3540 }
3641
42+ for (int idx =0 ; idx <=ch1 .length ; idx ++) {
43+ LISArr [idx ][0 ] = 0 ;
44+ }
3745
38- // ์ฌ๊ธฐ์๋ถํฐ LIS ๋ฅผ ๊ตฌํ ๊ฒ์
39- List < Integer > indices = new ArrayList <>();
40- for (int index = 0 ; index < chText2 .length ; index ++) {
46+ int maxLIS = 0 ;
47+ for ( int index1 = 0 ; index1 < ch1 . length ; index1 ++) {
48+ for (int index2 = 0 ; index2 < ch2 .length ; index2 ++) {
4149
42- char find = chText2 [index ];
43- if (positionIndices [find -'a' ] != null && positionIndices [find -'a' ].size () > 0 ) {
44- // ์ญ์ (LIS ๋ฅผ ๊ตฌํ๊ธฐ ์ํด์ ์ผ๋ถ๋ฌ ๋ค์ง์ด ๋์, ์ฆ ๊ฐ char ๋ณ LIS ๋ฅผ ๊ตฌํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์..)
45- // positionIndices ์์ ๊ตฌํ๋ ๊ฐ๋ค์ ๊ทธ๋๋ก addAll ํ๋ค๋ฉด ์ค๋ฆ์ฐจ์์ด ๋๊ธฐ๋๋ฌธ์ ์ ํํ LIS ๋ฅผ ๊ตฌํ ์ ์๋ค
50+ // if same, then
51+ if (ch1 [index1 ] == ch2 [index2 ]) {
52+ // LISArr ์ ์ธ๋ฑ์ค๊ฐ์ด +1 ์ด ๋๋ ์ด์ ๋ LISArr ์ ํฌ๊ธฐ๊ฐ text1, text2 ํฌ๊ธฐ + 1 ๋งํผ ๋ง๋ค์๊ธฐ ๋๋ฌธ
53+ LISArr [index1 + 1 ][index2 + 1 ] = LISArr [index1 ][index2 ] + 1 ;
54+ } else {
55+ int biggerOne = Math .max (LISArr [index1 ][index2 + 1 ], LISArr [index1 + 1 ][index2 ]);
56+ LISArr [index1 + 1 ][index2 + 1 ] = biggerOne ;
57+ }
4658
47- indices . addAll ( positionIndices [ find - 'a' ]. stream (). sorted ( Comparator . reverseOrder ()). toList () );
59+ maxLIS = Math . max ( maxLIS , LISArr [ index1 + 1 ][ index2 + 1 ] );
4860 }
4961 }
5062
51- // find LIS
52- return findLIS (indices ).size ();
63+ return maxLIS ;
64+
65+
66+
67+ /**
68+ * LIS, LCS ์ ์ฐ๊ด์ง์ด์ ํ์ด
69+ * 1. ๋จผ์ text1์ ํ์ด๋ณด๋๋ฐ text1์ ์ค๋ณต๋๋ ์ํ๋ฒณ์ด ์์ ์ ์๊ธฐ์ ๊ฐ ์บ๋ฆญํฐ๋ณ๋ก ์ธ๋ฑ์ค๋ค์ ์ ์ฅ (๊ฐ ์์๊ฐ arrayList๊ฐ ์๋ ๋ฐฐ์ด)
70+ * 2. text2 ๋ฅผ ํ์ผ๋ฉด์ ๋งค์นญ๋๋ ์บ๋ฆญํฐ๋ค์ ๋ํด์๋ง 1์์ ์ ์ฅ๋ ์ธ๋ฑ์ค๋ค์ ํ ๋ฐฐ์ด์์ ๋์ด
71+ * -> ์ด๋ ๋์ดํด์ ๋ฃ์๋๋ง๋ค ์ญ์์ผ๋ก ์ง์ด๋ฃ๋๊ฒ ์ค์! (์๋๋ฉด ์ด ๋์ด๋ ์ธ๋ฑ์ค๋ค์ ๊ฐ์ง๊ณ LIS๋ฅผ ๊ตฌํ ๊ฑฐ๋ผ์)
72+ * 3. ๋์ด๋ ์ธ๋ฑ์ค๋ค์ ๊ฐ๋ค์ ๊ฐ์ง๊ณ LIS ๋ฅผ ๊ตฌํ๋ค, ์ฆ ์ด LIS์ ๊ธธ์ด๊ฐ LCS ์ ๊ธธ์ด๊ฐ ๋๋ค..!! (์์ฐ ์ ๊ธฐ)
73+ * ๊ทธ๋ฌ๋ leet code ์ ๋๋ ธ์ ๋ runtime ์ด ์ ์ข์ง๋ ์์
74+ *
75+ * time complexity : O(M + N logN) => text2์ ๋ํด์ ๋ฌธ์๋ง๋ค ๋ฐ์ด๋๋ฆฌ์์น๊ฐ ์ํ๋จ
76+ * space complexity : O(M+N)
77+ */
78+ // char[] chText1 = text1.toCharArray();
79+ // char[] chText2 = text2.toCharArray();
80+ //
81+ // // text1๋ง ๊ฐ ์์ ๋ณ๋ก char๊ฐ ๊ฐ์ง๋ index๋ฅผ array ๋ก ๊ฐ๋ ๋ฐฐ์ด์ ์์ฑ
82+ // List<Integer>[] positionIndices = new List[26];
83+ // for (int index = 0; index<chText1.length; index++) {
84+ // if (positionIndices[chText1[index] - 'a'] == null) {
85+ // positionIndices[chText1[index] - 'a'] = new ArrayList();
86+ // positionIndices[chText1[index] - 'a'].add(index);
87+ // } else {
88+ // positionIndices[chText1[index] - 'a'].add(index);
89+ // }
90+ // }
91+ //
92+ //
93+ // // ์ฌ๊ธฐ์๋ถํฐ LIS ๋ฅผ ๊ตฌํ ๊ฒ์
94+ // List<Integer> indices = new ArrayList<>();
95+ // for (int index=0; index<chText2.length; index++) {
96+ //
97+ // char find = chText2[index];
98+ // if (positionIndices[find-'a'] != null && positionIndices[find-'a'].size() > 0) {
99+ // // ์ญ์ (LIS ๋ฅผ ๊ตฌํ๊ธฐ ์ํด์ ์ผ๋ถ๋ฌ ๋ค์ง์ด ๋์, ์ฆ ๊ฐ char ๋ณ LIS ๋ฅผ ๊ตฌํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์..)
100+ // // positionIndices ์์ ๊ตฌํ๋ ๊ฐ๋ค์ ๊ทธ๋๋ก addAll ํ๋ค๋ฉด ์ค๋ฆ์ฐจ์์ด ๋๊ธฐ๋๋ฌธ์ ์ ํํ LIS ๋ฅผ ๊ตฌํ ์ ์๋ค
101+ //
102+ // indices.addAll(positionIndices[find-'a'].stream().sorted(Comparator.reverseOrder()).toList());
103+ // }
104+ // }
105+ //
106+ // // find LIS
107+ // return findLIS(indices).size();
53108 }
54109
55110 public List <Integer > findLIS (List <Integer > source ) {
0 commit comments