Skip to content

Commit 2630a41

Browse files
authored
Create 0516-longest-palindromic-subsequence.kt
1 parent 5651c8c commit 2630a41

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
fun longestPalindromeSubseq(s1: String): Int {
3+
val s2 = s1.reversed()
4+
val n = s1.length
5+
val m = s2.length
6+
val dp = Array(n+1){ IntArray(m+1) }
7+
8+
for (i in 0 until n) {
9+
for (j in 0 until m) {
10+
if(s1[i] == s2[j])
11+
dp[i+1][j+1] = 1 + dp[i][j]
12+
else
13+
dp[i+1][j+1] = maxOf(dp[i][j+1], dp[i+1][j])
14+
}
15+
}
16+
17+
return dp[n][m]
18+
}
19+
}

0 commit comments

Comments
 (0)