Skip to content

Commit ea57cdf

Browse files
committed
feat: add 290
1 parent a315c84 commit ea57cdf

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

docs/1-100/290.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 290 单词规律
2+
3+
## 题目
4+
5+
给定一种规律 pattern  和一个字符串  s ,判断 s  是否遵循相同的规律。
6+
7+
这里的   遵循   指完全匹配,例如, pattern  里的每个字母和字符串  s  中的每个非空单词之间存在着双向连接的对应规律。
8+
9+
示例 1:
10+
11+
```js
12+
输入: (pattern = "abba"), (s = "dog cat cat dog");
13+
输出: true;
14+
```
15+
16+
示例 2:
17+
18+
```js
19+
输入: (pattern = "abba"), (s = "dog cat cat fish");
20+
输出: false;
21+
```
22+
23+
示例 3:
24+
25+
```js
26+
输入: (pattern = "aaaa"), (s = "dog cat cat dog");
27+
输出: false;
28+
```
29+
30+
提示:
31+
32+
1 <= pattern.length <= 300
33+
pattern  只包含小写英文字母
34+
1 <= s.length <= 3000
35+
s  只包含小写英文字母和  ' '
36+
s  不包含 任何前导或尾随对空格
37+
s  中每个单词都被 单个空格 分隔
38+
39+
## 题解
40+
41+
1.
42+
43+
:::tip
44+
<runtime :list="[60, 68.48, 40.9, 63.31]" />
45+
:::
46+
47+
```js
48+
/**
49+
* @param {string} pattern
50+
* @param {string} s
51+
* @return {boolean}
52+
*/
53+
var wordPattern = function (pattern, s) {
54+
let map = {};
55+
let list = [];
56+
const wordList = s.split(" ");
57+
const patternLen = pattern.length;
58+
59+
if (patternLen !== wordList.length) return false;
60+
61+
for (let i = 0; i < patternLen; i++) {
62+
const currentStr = pattern[i];
63+
const currentWord = wordList[i];
64+
65+
if (map[currentStr]) {
66+
if (map[currentStr] !== currentWord) return false;
67+
} else {
68+
if (list.includes(currentWord)) return false;
69+
map[currentStr] = currentWord;
70+
list.push(currentWord);
71+
}
72+
}
73+
74+
return true;
75+
};
76+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy