FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Go/dynamic/longestcommonsubsequence.go at master · gitgitcode/Go · GitHub
gitgitcode
/
Go
Public
forked from
TheAlgorithms/Go
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Go
/
dynamic
/
longestcommonsubsequence.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
40 lines (34 loc) · 1.01 KB
Breadcrumbs
Go
/
dynamic
/
longestcommonsubsequence.go
Copy path
File metadata and controls
40 lines (34 loc) · 1.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// LONGEST COMMON SUBSEQUENCE
// DP - 4
// https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/
package
dynamic
// LongestCommonSubsequence function
func
LongestCommonSubsequence
(
a
string
,
b
string
,
m
int
,
n
int
)
int
{
// m is the length of string a and n is the length of string b
// here we are making a 2d slice of size (m+1)*(n+1)
lcs
:=
make
([][]
int
,
m
+
1
)
for
i
:=
0
;
i
<=
m
;
i
++
{
lcs
[
i
]
=
make
([]
int
,
n
+
1
)
}
// block that implements LCS
for
i
:=
0
;
i
<=
m
;
i
++
{
for
j
:=
0
;
j
<=
n
;
j
++
{
if
i
==
0
||
j
==
0
{
lcs
[
i
][
j
]
=
0
}
else
if
a
[
i
-
1
]
==
b
[
j
-
1
] {
lcs
[
i
][
j
]
=
lcs
[
i
-
1
][
j
-
1
]
+
1
}
else
{
lcs
[
i
][
j
]
=
Max
(
lcs
[
i
-
1
][
j
],
lcs
[
i
][
j
-
1
])
}
}
}
// returning the length of longest common subsequence
return
lcs
[
m
][
n
]
}
// func main(){
// // declaring two strings and asking for input
// var a,b string
// fmt.Scan(&a, &b)
// // calling the LCS function
// fmt.Println("The length of longest common subsequence is:", longestCommonSubsequence(a,b, len(a), len(b)))
// }
Back
|
FazBrowse Home
|
New Git URL