We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e1fbc0d commit 2808f12Copy full SHA for 2808f12
828-unique-letter-string.js
@@ -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
25
/**
26
* @param {string} S
27
* @return {number}
0 commit comments