File tree 2 files changed +21
-9
lines changed
main/ts/g0201_0300/s0290_word_pattern
test/ts/g0201_0300/s0290_word_pattern
2 files changed +21
-9
lines changed Original file line number Diff line number Diff line change 2
2
// #2025_04_12_Time_0_ms_(100.00%)_Space_55.01_MB_(70.10%)
3
3
4
4
function wordPattern ( pattern : string , s : string ) : boolean {
5
- const map = new Map < string , string > ( ) ;
6
- const words = s . split ( " " ) ;
5
+ const map = new Map < string , string > ( )
6
+ const words = s . split ( ' ' )
7
7
if ( words . length !== pattern . length ) {
8
- return false ;
8
+ return false
9
9
}
10
10
for ( let i = 0 ; i < pattern . length ; i ++ ) {
11
- const char = pattern [ i ] ;
12
- const word = words [ i ] ;
11
+ const char = pattern [ i ]
12
+ const word = words [ i ]
13
13
if ( ! map . has ( char ) ) {
14
14
if ( [ ...map . values ( ) ] . includes ( word ) ) {
15
- return false ;
15
+ return false
16
16
}
17
- map . set ( char , word ) ;
17
+ map . set ( char , word )
18
18
} else {
19
19
if ( map . get ( char ) !== word ) {
20
- return false ;
20
+ return false
21
21
}
22
22
}
23
23
}
24
- return true ;
24
+ return true
25
25
}
26
26
27
27
export { wordPattern }
Original file line number Diff line number Diff line change @@ -13,3 +13,15 @@ test('wordPattern2', () => {
13
13
test ( 'wordPattern3' , ( ) => {
14
14
expect ( wordPattern ( 'aaaa' , 'dog cat cat dog' ) ) . toEqual ( false )
15
15
} )
16
+
17
+ test ( 'wordPattern4' , ( ) => {
18
+ expect ( wordPattern ( 'a' , 'dog cat' ) ) . toEqual ( false )
19
+ } )
20
+
21
+ test ( 'wordPattern5' , ( ) => {
22
+ expect ( wordPattern ( 'a' , 'dog' ) ) . toEqual ( true )
23
+ } )
24
+
25
+ test ( 'wordPattern6' , ( ) => {
26
+ expect ( wordPattern ( 'ab' , 'dog dog' ) ) . toEqual ( false )
27
+ } )
You can’t perform that action at this time.
0 commit comments