MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / LpsDp

Function LpsDp

dynamic/longestpalindromicsubsequence.go:27–53  ·  view source on GitHub ↗

LpsDp function

(word string)

Source from the content-addressed store, hash-verified

25
26// LpsDp function
27func LpsDp(word string) int {
28 N := len(word)
29 dp := make([][]int, N)
30
31 for i := 0; i < N; i++ {
32 dp[i] = make([]int, N)
33 dp[i][i] = 1
34 }
35
36 for l := 2; l <= N; l++ {
37 // for length l
38 for i := 0; i < N-l+1; i++ {
39 j := i + l - 1
40 if word[i] == word[j] {
41 if l == 2 {
42 dp[i][j] = 2
43 } else {
44 dp[i][j] = 2 + dp[i+1][j-1]
45 }
46 } else {
47 dp[i][j] = Max(dp[i+1][j], dp[i][j-1])
48 }
49 }
50 }
51
52 return dp[0][N-1]
53}

Callers

nothing calls this directly

Calls 1

MaxFunction · 0.85

Tested by

no test coverage detected