File tree Expand file tree Collapse file tree 1 file changed +13
-12
lines changed Expand file tree Collapse file tree 1 file changed +13
-12
lines changed Original file line number Diff line number Diff line change 2
2
* @param {string } s
3
3
* @return {number }
4
4
*/
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
9
8
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
17
16
}
18
- return ans
17
+
18
+ return res
19
19
}
20
20
21
+
21
22
// another
22
23
23
24
/**
You can’t perform that action at this time.
0 commit comments