Skip to content

Commit 6c0406c

Browse files
committedMar 10, 2020
提交 290 单词规律
1 parent 59d35f1 commit 6c0406c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
 

‎290.单词规律/solution.html

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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>

0 commit comments

Comments
 (0)
Please sign in to comment.