Skip to content

Commit 2457263

Browse files
authored
Update 2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js
1 parent f106183 commit 2457263

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,47 @@ function palindromic( i, s) {
2727
}
2828
return len;
2929
}
30+
31+
// another
32+
33+
/**
34+
* @param {string} s
35+
* @return {number}
36+
*/
37+
const maxProduct = function(s) {
38+
const s1 = [], s2 = [], n = s.length
39+
let res = 0
40+
dfs(0)
41+
return res
42+
43+
function dfs(idx) {
44+
if(idx === n) {
45+
if(isPalindromic(s1) && isPalindromic(s2)) {
46+
res = Math.max(res, s1.length * s2.length)
47+
}
48+
return
49+
}
50+
const ch = s[idx]
51+
s1.push(ch)
52+
dfs(idx + 1)
53+
s1.pop()
54+
55+
s2.push(ch)
56+
dfs(idx + 1)
57+
s2.pop()
58+
59+
dfs(idx + 1)
60+
}
61+
function isPalindromic(arr) {
62+
let l = 0, r = arr.length - 1
63+
while(l < r) {
64+
if(arr[l] === arr[r]) {
65+
l++
66+
r--
67+
} else {
68+
return false
69+
}
70+
}
71+
return true
72+
}
73+
};

0 commit comments

Comments
 (0)