Skip to content

Commit 2690219

Browse files
authored
Create 2002-maximum-product-of-the-length-of-two-palindromic-subsequences.kt
1 parent ba64c68 commit 2690219

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
fun maxProduct(s: String): Int {
3+
val hm = HashMap<Int, Int>()
4+
5+
for (mask in 1 until (1 shl s.length)) {
6+
val sb = StringBuilder()
7+
8+
for (i in 0 until s.length) {
9+
if (mask and (1 shl i) > 0) {
10+
sb.append(s[i])
11+
}
12+
}
13+
14+
val p = sb.toString()
15+
if (p == p.reversed()) {
16+
hm.put(mask, p.length)
17+
}
18+
}
19+
20+
var max = 0
21+
for (mask1 in hm.keys) {
22+
for (mask2 in hm.keys) {
23+
if (mask1 and mask2 == 0) {
24+
max = maxOf(
25+
max,
26+
hm[mask1]!! * hm[mask2]!!
27+
)
28+
}
29+
}
30+
}
31+
32+
return max
33+
}
34+
}

0 commit comments

Comments
 (0)