Maximize Number of Subsequences in a String LeetCode solution in JavaScript

1
/**
2
* @param {string} text
3
* @param {string} pattern
4
* @return {number}
5
*/
6
var maximumSubsequenceCount = function (text, pattern) {
7
const arrText = text.split("");
8
const lengthP0 = arrText.filter((x) => x === pattern[0]).length;
9
const lengthP1 = arrText.filter((x) => x === pattern[1]).length;
10
const [c1, c2, lengthmax] =
11
lengthP0 <= lengthP1
12
? [...pattern, lengthP1]
13
: [pattern[1], pattern[0], lengthP0];
14
let newText = lengthP0 <= lengthP1 ? [c1, ...arrText] : [...arrText, c1];
15
newText = lengthP0 <= lengthP1 ? newText : newText.reverse();
16

17
let i = 0;
18
let count = 0;
19
let countmax = lengthmax;
20
while (i < newText.length) {
21
if (newText[i] === c1) {
22
count += countmax;
23
}
24
if (newText[i] === c2) {
25
countmax--;
26
}
27
i++;
28
}
29
return count;
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0