File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
number-of-matching-subsequences Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ Step 2. Add the dependency
45
45
46
46
<summary >展开查看</summary >
47
47
48
+ https://leetcode.cn/problems/number-of-matching-subsequences
49
+
48
50
https://leetcode.cn/problems/global-and-local-inversions
49
51
50
52
https://leetcode.cn/problems/maximum-units-on-a-truck
Original file line number Diff line number Diff line change
1
+ import { counter } from "../substring-with-concatenation-of-all-words/counter.ts" ;
2
+
3
+ function numMatchingSubseq ( s : string , words : string [ ] ) : number {
4
+ const cnt = counter ( words ) ;
5
+ const arr = Array ( 26 )
6
+ . fill ( 0 )
7
+ . map ( ( ) => new Set < [ string , number ] > ( ) ) ;
8
+
9
+ for ( const [ w , count ] of cnt ) {
10
+ arr [ w . charCodeAt ( 0 ) - "a" . charCodeAt ( 0 ) ] . add ( [ w , count ] ) ;
11
+ }
12
+ let res = 0 ;
13
+ for ( const c of s ) {
14
+ const temp = arr [ c . charCodeAt ( 0 ) - "a" . charCodeAt ( 0 ) ] ;
15
+ for ( const wordcount of temp ) {
16
+ const [ word , count1 ] = wordcount ;
17
+ if ( word . length === 1 ) {
18
+ res += count1 ;
19
+ temp . delete ( wordcount ) ;
20
+ } else {
21
+ const w1 = word . slice ( 1 ) ;
22
+ if ( w1 . charCodeAt ( 0 ) === word . charCodeAt ( 0 ) ) {
23
+ wordcount [ 0 ] = word . slice ( 1 ) ;
24
+ } else {
25
+ temp . delete ( wordcount ) ;
26
+ arr [ w1 . charCodeAt ( 0 ) - "a" . charCodeAt ( 0 ) ] . add ( [ w1 , count1 ] ) ;
27
+ }
28
+ }
29
+ }
30
+ }
31
+
32
+ return res ;
33
+ }
34
+ export default numMatchingSubseq ;
You can’t perform that action at this time.
0 commit comments