File tree 3 files changed +129
-0
lines changed
solution/2900-2999/2949.Count Beautiful Substrings II
3 files changed +129
-0
lines changed Original file line number Diff line number Diff line change 74
74
75
75
## 解法
76
76
77
+ ### 方法一:前缀和 + 哈希表 + 分解质因子
78
+
79
+ <!-- tabs:start -->
80
+
81
+ ``` ts
82
+ function beautifulSubstrings(s : string , k : number ): number {
83
+ const l = pSqrt (k * 4 );
84
+ const n = s .length ;
85
+ let sum = n ;
86
+ let ans = 0 ;
87
+ const counter = new Map ();
88
+ counter .set (((l - 1 ) << 17 ) | sum , 1 );
89
+ for (let i = 0 ; i < n ; i ++ ) {
90
+ const char = s [i ];
91
+ const bit = (AEIOU_MASK >> (char .charCodeAt (0 ) - ' a' .charCodeAt (0 ))) & 1 ;
92
+ sum += bit * 2 - 1 ; // 1 -> 1 0 -> -1
93
+ const key = (i % l << 17 ) | sum ;
94
+ ans += counter .get (key ) || 0 ; // ans += cnt[(i%k,sum)]++
95
+ counter .set (key , (counter .get (key ) ?? 0 ) + 1 );
96
+ }
97
+ return ans ;
98
+ }
99
+ const AEIOU_MASK = 1065233 ;
100
+
101
+ function pSqrt(n : number ) {
102
+ let res = 1 ;
103
+ for (let i = 2 ; i * i <= n ; i ++ ) {
104
+ let i2 = i * i ;
105
+ while (n % i2 == 0 ) {
106
+ res *= i ;
107
+ n /= i2 ;
108
+ }
109
+ if (n % i == 0 ) {
110
+ res *= i ;
111
+ n /= i ;
112
+ }
113
+ }
114
+ if (n > 1 ) {
115
+ res *= n ;
116
+ }
117
+ return res ;
118
+ }
119
+ ```
120
+
121
+ <!-- tabs:end -->
122
+
77
123
<!-- end -->
Original file line number Diff line number Diff line change @@ -70,4 +70,50 @@ It can be shown that there are only 3 beautiful substrings in the given string.
70
70
71
71
## Solutions
72
72
73
+ ### Solution 1: Prefix Sum + Hash Table
74
+
75
+ <!-- tabs:start -->
76
+
77
+ ``` ts
78
+ function beautifulSubstrings(s : string , k : number ): number {
79
+ const l = pSqrt (k * 4 );
80
+ const n = s .length ;
81
+ let sum = n ;
82
+ let ans = 0 ;
83
+ const counter = new Map ();
84
+ counter .set (((l - 1 ) << 17 ) | sum , 1 );
85
+ for (let i = 0 ; i < n ; i ++ ) {
86
+ const char = s [i ];
87
+ const bit = (AEIOU_MASK >> (char .charCodeAt (0 ) - ' a' .charCodeAt (0 ))) & 1 ;
88
+ sum += bit * 2 - 1 ; // 1 -> 1 0 -> -1
89
+ const key = (i % l << 17 ) | sum ;
90
+ ans += counter .get (key ) || 0 ; // ans += cnt[(i%k,sum)]++
91
+ counter .set (key , (counter .get (key ) ?? 0 ) + 1 );
92
+ }
93
+ return ans ;
94
+ }
95
+ const AEIOU_MASK = 1065233 ;
96
+
97
+ function pSqrt(n : number ) {
98
+ let res = 1 ;
99
+ for (let i = 2 ; i * i <= n ; i ++ ) {
100
+ let i2 = i * i ;
101
+ while (n % i2 == 0 ) {
102
+ res *= i ;
103
+ n /= i2 ;
104
+ }
105
+ if (n % i == 0 ) {
106
+ res *= i ;
107
+ n /= i ;
108
+ }
109
+ }
110
+ if (n > 1 ) {
111
+ res *= n ;
112
+ }
113
+ return res ;
114
+ }
115
+ ```
116
+
117
+ <!-- tabs:end -->
118
+
73
119
<!-- end -->
Original file line number Diff line number Diff line change
1
+ function beautifulSubstrings ( s : string , k : number ) : number {
2
+ const l = pSqrt ( k * 4 ) ;
3
+ const n = s . length ;
4
+ let sum = n ;
5
+ let ans = 0 ;
6
+ const counter = new Map ( ) ;
7
+ counter . set ( ( ( l - 1 ) << 17 ) | sum , 1 ) ;
8
+ for ( let i = 0 ; i < n ; i ++ ) {
9
+ const char = s [ i ] ;
10
+ const bit = ( AEIOU_MASK >> ( char . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ) ) & 1 ;
11
+ sum += bit * 2 - 1 ; // 1 -> 1 0 -> -1
12
+ const key = ( i % l << 17 ) | sum ;
13
+ ans += counter . get ( key ) || 0 ; // ans += cnt[(i%k,sum)]++
14
+ counter . set ( key , ( counter . get ( key ) ?? 0 ) + 1 ) ;
15
+ }
16
+ return ans ;
17
+ }
18
+ const AEIOU_MASK = 1065233 ;
19
+
20
+ function pSqrt ( n : number ) {
21
+ let res = 1 ;
22
+ for ( let i = 2 ; i * i <= n ; i ++ ) {
23
+ let i2 = i * i ;
24
+ while ( n % i2 == 0 ) {
25
+ res *= i ;
26
+ n /= i2 ;
27
+ }
28
+ if ( n % i == 0 ) {
29
+ res *= i ;
30
+ n /= i ;
31
+ }
32
+ }
33
+ if ( n > 1 ) {
34
+ res *= n ;
35
+ }
36
+ return res ;
37
+ }
You can’t perform that action at this time.
0 commit comments