File tree Expand file tree Collapse file tree 2 files changed +21
-9
lines changed
main/ts/g0201_0300/s0290_word_pattern
test/ts/g0201_0300/s0290_word_pattern Expand file tree Collapse file tree 2 files changed +21
-9
lines changed Original file line number Diff line number Diff line change 22// #2025_04_12_Time_0_ms_(100.00%)_Space_55.01_MB_(70.10%)
33
44function 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 ( ' ' )
77 if ( words . length !== pattern . length ) {
8- return false ;
8+ return false
99 }
1010 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 ]
1313 if ( ! map . has ( char ) ) {
1414 if ( [ ...map . values ( ) ] . includes ( word ) ) {
15- return false ;
15+ return false
1616 }
17- map . set ( char , word ) ;
17+ map . set ( char , word )
1818 } else {
1919 if ( map . get ( char ) !== word ) {
20- return false ;
20+ return false
2121 }
2222 }
2323 }
24- return true ;
24+ return true
2525}
2626
2727export { wordPattern }
Original file line number Diff line number Diff line change @@ -13,3 +13,15 @@ test('wordPattern2', () => {
1313test ( 'wordPattern3' , ( ) => {
1414 expect ( wordPattern ( 'aaaa' , 'dog cat cat dog' ) ) . toEqual ( false )
1515} )
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