Skip to content

Commit 2808f12

Browse files
authored
Update 828-unique-letter-string.js
1 parent e1fbc0d commit 2808f12

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

828-unique-letter-string.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const uniqueLetterString = function(s) {
6+
const n = s.length
7+
const arr = Array.from({ length: 26 }, () => Array(2).fill(-1))
8+
const A = 'A'.charCodeAt(0)
9+
let res = 0
10+
for(let i = 0; i < n; i++) {
11+
const idx = s.charCodeAt(i) - A
12+
res += (i - arr[idx][1]) * (arr[idx][1] - arr[idx][0])
13+
arr[idx] = [arr[idx][1], i]
14+
}
15+
16+
for(let i = 0; i < 26; i++) {
17+
res += (n - arr[i][1]) * (arr[i][1] - arr[i][0])
18+
}
19+
20+
return res
21+
};
22+
23+
// another
24+
125
/**
226
* @param {string} S
327
* @return {number}

0 commit comments

Comments
 (0)