Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7815a51

Browse files
authoredJan 5, 2019
Create 290-word-pattern.js
1 parent 1c40da0 commit 7815a51

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 

‎290-word-pattern.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} pattern
3+
* @param {string} str
4+
* @return {boolean}
5+
*/
6+
const wordPattern = function(pattern, str) {
7+
const pm = {}
8+
const sm = {}
9+
const sa = str.trim().split(' ')
10+
if(pattern.length !== sa.length) return false
11+
for(let i = 0; i< pattern.length; i++) {
12+
if(!pm.hasOwnProperty(pattern[i])) {
13+
pm[pattern[i]] = sa[i]
14+
}
15+
if(!sm.hasOwnProperty(sa[i])) {
16+
sm[sa[i]] = pattern[i]
17+
}
18+
19+
if( !(pm[pattern[i]] === sa[i] && sm[sa[i]] === pattern[i] ) ) {
20+
return false
21+
}
22+
23+
}
24+
return true
25+
};

0 commit comments

Comments
 (0)
Please sign in to comment.