Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6731205

Browse files
committed
Add solution #2002
1 parent 2c23301 commit 6731205

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,680 LeetCode solutions in JavaScript
1+
# 1,681 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1536,6 +1536,7 @@
15361536
1998|[GCD Sort of an Array](./solutions/1998-gcd-sort-of-an-array.js)|Hard|
15371537
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
15381538
2001|[Number of Pairs of Interchangeable Rectangles](./solutions/2001-number-of-pairs-of-interchangeable-rectangles.js)|Medium|
1539+
2002|[Maximum Product of the Length of Two Palindromic Subsequences](./solutions/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js)|Medium|
15391540
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15401541
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15411542
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* 2002. Maximum Product of the Length of Two Palindromic Subsequences
3+
* https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, find two disjoint palindromic subsequences of s such that the product of their
7+
* lengths is maximized. The two subsequences are disjoint if they do not both pick a character at
8+
* the same index.
9+
*
10+
* Return the maximum possible product of the lengths of the two palindromic subsequences.
11+
*
12+
* A subsequence is a string that can be derived from another string by deleting some or no
13+
* characters without changing the order of the remaining characters. A string is palindromic
14+
* if it reads the same forward and backward.
15+
*/
16+
17+
/**
18+
* @param {string} s
19+
* @return {number}
20+
*/
21+
var maxProduct = function(s) {
22+
const n = s.length;
23+
let result = 0;
24+
25+
for (let mask1 = 1; mask1 < (1 << n); mask1++) {
26+
for (let mask2 = 1; mask2 < (1 << n); mask2++) {
27+
if (mask1 & mask2) continue;
28+
const len1 = isPalindrome(s, mask1);
29+
if (len1 === 0) continue;
30+
const len2 = isPalindrome(s, mask2);
31+
if (len2 === 0) continue;
32+
result = Math.max(result, len1 * len2);
33+
}
34+
}
35+
36+
return result;
37+
38+
function isPalindrome(str, mask) {
39+
const chars = [];
40+
for (let i = 0; i < n; i++) {
41+
if (mask & (1 << i)) chars.push(str[i]);
42+
}
43+
let left = 0;
44+
let right = chars.length - 1;
45+
while (left < right) {
46+
if (chars[left++] !== chars[right--]) return 0;
47+
}
48+
return chars.length;
49+
}
50+
};

0 commit comments

Comments
 (0)