Skip to content

Commit

Permalink
Merge b2f9d36 into 67eee1d
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Dec 20, 2019
2 parents 67eee1d + b2f9d36 commit 7089979
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,5 @@ problems from
* [Day 393](https://github.com/vaskoz/dailycodingproblem-go/issues/789)
* [Day 394](https://github.com/vaskoz/dailycodingproblem-go/issues/791)
* [Day 395](https://github.com/vaskoz/dailycodingproblem-go/issues/793)
* [Day 396](https://github.com/vaskoz/dailycodingproblem-go/issues/795)

67 changes: 67 additions & 0 deletions day396/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package day396

// LongestPalindromicSubsequenceBrute returns the Longest
// Palindromic Subsequence using brute force.
func LongestPalindromicSubsequenceBrute(str string) int {
runes := []rune(str)

return lpsBrute(runes)
}

func lpsBrute(r []rune) int {
switch l := len(r); {
case l == 1:
return 1
case l == 2 && r[0] == r[1]:
return 2
case r[0] == r[l-1]:
return lpsBrute(r[1:l-1]) + 2
default:
return max(
lpsBrute(r[:l-1]),
lpsBrute(r[1:l]),
)
}
}

func max(a, b int) int {
if a > b {
return a
}

return b
}

// LongestPalindromicSubsequenceDP returns the Longest
// Palindromic Subsequence using Dynamic Programming.
// Runs in O(N^2) space and time.
func LongestPalindromicSubsequenceDP(str string) int {
runes := []rune(str)

return lpsDP(runes)
}

func lpsDP(r []rune) int {
n := len(r)
subprob := make([][]int, n)

for i := range subprob {
subprob[i] = make([]int, n)
subprob[i][i] = 1
}

for length := 2; length <= n; length++ {
for i := 0; i <= n-length; i++ {
switch j := i + length - 1; {
case r[i] == r[j] && length == 2:
subprob[i][j] = 2
case r[i] == r[j]:
subprob[i][j] = subprob[i+1][j-1] + 2
default:
subprob[i][j] = max(subprob[i][j-1], subprob[i+1][j])
}
}
}

return subprob[0][n-1]
}
48 changes: 48 additions & 0 deletions day396/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package day396

import "testing"

// nolint
var testcases = []struct {
str string
expected int
}{
{"MAPTPTMTPA", 7},
{"ABFOOBARRABOOFYZ", 12},
}

func TestLongestPalindromicSubsequenceBrute(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if res := LongestPalindromicSubsequenceBrute(tc.str); res != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, res)
}
}
}

func BenchmarkLongestPalindromicSubsequenceBrute(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
LongestPalindromicSubsequenceBrute(tc.str)
}
}
}

func TestLongestPalindromicSubsequenceDP(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if res := LongestPalindromicSubsequenceDP(tc.str); res != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, res)
}
}
}

func BenchmarkLongestPalindromicSubsequenceDP(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
LongestPalindromicSubsequenceDP(tc.str)
}
}
}

0 comments on commit 7089979

Please sign in to comment.