Skip to content

Commit 105a145

Browse files
committed
feat: leetcode 828. Count Unique Characters of All Substrings of a Given String
1 parent 352743c commit 105a145

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const uniqueLetterString = function (s) {
6+
const n = s.length; let ans = 0
7+
for (let i = 0; i < 26; i++) {
8+
const ch = String.fromCharCode('A'.charCodeAt(0) + i)
9+
const pos = []
10+
for (let i = 0; i < n; i++) {
11+
if (s[i] === ch) pos.push(i)
12+
}
13+
for (let j = 0; j < pos.length; j++) {
14+
const l = pos[j] - (pos[j - 1] ?? -1); const r = (pos[j + 1] ?? n) - pos[j]
15+
ans += l * r
16+
}
17+
}
18+
return ans
19+
}

0 commit comments

Comments
 (0)