Skip to content

Commit e1fbc0d

Browse files
authored
Update 2262-total-appeal-of-a-string.js
1 parent 47cdc19 commit e1fbc0d

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

2262-total-appeal-of-a-string.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
* @param {string} s
33
* @return {number}
44
*/
5-
var appealSum = function (s) {
6-
const pos = Array(26).fill(-1)
7-
let ans = 0
8-
const n = s.length
5+
const appealSum = function (s) {
6+
const n = s.length, pos = Array(26).fill(-1)
7+
let res = 0
98
const a = 'a'.charCodeAt(0)
10-
for (let i = 0; i < n; i++) {
11-
let tmp = n - i
12-
if (pos[s.charCodeAt(i) - a] !== -1)
13-
tmp += (i - pos[s.charCodeAt(i) - a] - 1) * (n - i)
14-
else tmp += i * (n - i)
15-
ans += tmp
16-
pos[s.charCodeAt(i) - a] = i
9+
for(let i = 0; i < n; i++) {
10+
let tmp = n - i, idx = s.charCodeAt(i) - a
11+
if(pos[idx] !== -1) {
12+
tmp += (i - pos[idx] - 1) * (n - i)
13+
} else tmp += i * (n - i)
14+
res += tmp
15+
pos[idx] = i
1716
}
18-
return ans
17+
18+
return res
1919
}
2020

21+
2122
// another
2223

2324
/**

0 commit comments

Comments
 (0)