File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
+ < title > Document</ title >
7
+ </ head >
8
+ < body >
9
+ < script >
10
+ // Source : https://leetcode.com/problems/word-pattern/
11
+ // Author : 悬笔e绝
12
+ // Date : 2020-03-10
13
+
14
+ /**
15
+ * @param {string } pattern
16
+ * @param {string } str
17
+ * @return {boolean }
18
+ */
19
+ var wordPattern = function ( pattern , str ) {
20
+ var arr = str . split ( ' ' ) ;
21
+ if ( pattern . length !== arr . length )
22
+ return false ;
23
+
24
+ var a2b = { }
25
+ , b2a = { } ;
26
+
27
+ for ( var i = 0 , len = pattern . length ; i < len ; i ++ ) {
28
+ var a = pattern [ i ]
29
+ , b = arr [ i ] ;
30
+ if ( ! a2b [ a ] ) {
31
+ a2b [ a ] = b ;
32
+ } else {
33
+ if ( a2b [ a ] !== b )
34
+ return false ;
35
+ }
36
+ if ( ! b2a [ b ] ) {
37
+ b2a [ b ] = a ;
38
+ } else {
39
+ if ( b2a [ b ] !== a )
40
+ return false ;
41
+ }
42
+ }
43
+ return true ;
44
+ } ;
45
+ </ script >
46
+ </ body >
47
+ </ html >
You can’t perform that action at this time.
0 commit comments