File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ // https://leetcode.com/problems/longest-common-subsequence/
4
+ func longestCommonSubsequence (text1 string , text2 string ) int {
5
+ var subseq [1000 ][1000 ]int
6
+ for i := 0 ; i < len (text1 ); i ++ {
7
+ for j := 0 ; j < len (text2 ); j ++ {
8
+ if text1 [i ] == text2 [j ] {
9
+ if i == 0 || j == 0 {
10
+ subseq [i ][j ] = 1
11
+ } else {
12
+ subseq [i ][j ] = subseq [i - 1 ][j - 1 ] + 1
13
+ }
14
+ } else {
15
+ if i == 0 && j != 0 {
16
+ subseq [i ][j ] = subseq [i ][j - 1 ]
17
+ } else if i != 0 && j == 0 {
18
+ subseq [i ][j ] = subseq [i - 1 ][j ]
19
+ } else if i != 0 && j != 0 && subseq [i - 1 ][j ] >= subseq [i ][j - 1 ] {
20
+ subseq [i ][j ] = subseq [i - 1 ][j ]
21
+ } else if i != 0 && j != 0 {
22
+ subseq [i ][j ] = subseq [i ][j - 1 ]
23
+ }
24
+ }
25
+ }
26
+ }
27
+ return subseq [len (text1 )- 1 ][len (text2 )- 1 ]
28
+ }
You can’t perform that action at this time.
0 commit comments