Skip to content

Commit aba5c1c

Browse files
authored
Create 828-unique-letter-string.js
1 parent b30b434 commit aba5c1c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

828-unique-letter-string.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} S
3+
* @return {number}
4+
*/
5+
const uniqueLetterString = function(S) {
6+
const len = S.length
7+
if (len === 0) return 0
8+
if (len === 1) return 1
9+
let count = 0
10+
let lastP = new Array(26).fill(0)
11+
let lastLastP = new Array(26).fill(0)
12+
let pre = 0
13+
for (let i = 0; i < len; i++) {
14+
let idx = S.charCodeAt(i) - 'A'.charCodeAt(0)
15+
pre += i - lastP[idx] + 1
16+
pre -= lastP[idx] - lastLastP[idx]
17+
count += pre
18+
lastLastP[idx] = lastP[idx]
19+
lastP[idx] = i + 1
20+
}
21+
return count % 1000000007
22+
}

0 commit comments

Comments
 (0)