Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 636e57b

Browse files
authored
Create 2272-substring-with-largest-variance.js
1 parent 4cecce4 commit 636e57b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const largestVariance = function (s) {
6+
const se = new Set(s),
7+
n = s.length
8+
let res = 0
9+
for (const x of se) {
10+
// max
11+
for (const y of se) {
12+
// min
13+
if (x != y) {
14+
let pre = Array(n + 1).fill(0),
15+
preX,
16+
preY,
17+
diff = 0
18+
for (let i = 0; i < n; i++) {
19+
if (s[i] == x) {
20+
preX = i + 1
21+
diff++
22+
}
23+
if (s[i] == y) {
24+
preY = i + 1
25+
diff--
26+
}
27+
pre[i + 1] = Math.min(pre[i], diff)
28+
if (preX == undefined || preY == undefined) continue
29+
res = Math.max(res, diff - pre[Math.min(preX, preY) - 1])
30+
}
31+
}
32+
}
33+
}
34+
return res
35+
}

0 commit comments

Comments
 (0)