Skip to content

Commit

Permalink
Create 2002-maximum-product-of-the-length-of-two-palindromic-subseque…
Browse files Browse the repository at this point in the history
…nces.kt
  • Loading branch information
a93a authored May 16, 2023
1 parent ba64c68 commit 2690219
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
fun maxProduct(s: String): Int {
val hm = HashMap<Int, Int>()

for (mask in 1 until (1 shl s.length)) {
val sb = StringBuilder()

for (i in 0 until s.length) {
if (mask and (1 shl i) > 0) {
sb.append(s[i])
}
}

val p = sb.toString()
if (p == p.reversed()) {
hm.put(mask, p.length)
}
}

var max = 0
for (mask1 in hm.keys) {
for (mask2 in hm.keys) {
if (mask1 and mask2 == 0) {
max = maxOf(
max,
hm[mask1]!! * hm[mask2]!!
)
}
}
}

return max
}
}

0 comments on commit 2690219

Please sign in to comment.