Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 431e025

Browse files
committed
commit solution 290
1 parent 0a2aaff commit 431e025

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [290. 单词规律](https://leetcode-cn.com/problems/word-pattern/description/)
2+
3+
### 题目描述
4+
5+
<p>给定一种规律 <code>pattern</code>&nbsp;和一个字符串&nbsp;<code>str</code>&nbsp;,判断 <code>str</code> 是否遵循相同的规律。</p>
6+
7+
<p>这里的&nbsp;<strong>遵循&nbsp;</strong>指完全匹配,例如,&nbsp;<code>pattern</code>&nbsp;里的每个字母和字符串&nbsp;<code>str</code><strong>&nbsp;</strong>中的每个非空单词之间存在着双向连接的对应规律。</p>
8+
9+
<p><strong>示例1:</strong></p>
10+
11+
<pre><strong>输入:</strong> pattern = <code>&quot;abba&quot;</code>, str = <code>&quot;dog cat cat dog&quot;</code>
12+
<strong>输出:</strong> true</pre>
13+
14+
<p><strong>示例 2:</strong></p>
15+
16+
<pre><strong>输入:</strong>pattern = <code>&quot;abba&quot;</code>, str = <code>&quot;dog cat cat fish&quot;</code>
17+
<strong>输出:</strong> false</pre>
18+
19+
<p><strong>示例 3:</strong></p>
20+
21+
<pre><strong>输入:</strong> pattern = <code>&quot;aaaa&quot;</code>, str = <code>&quot;dog cat cat dog&quot;</code>
22+
<strong>输出:</strong> false</pre>
23+
24+
<p><strong>示例&nbsp;4:</strong></p>
25+
26+
<pre><strong>输入:</strong> pattern = <code>&quot;abba&quot;</code>, str = <code>&quot;dog dog dog dog&quot;</code>
27+
<strong>输出:</strong> false</pre>
28+
29+
<p><strong>说明:</strong><br>
30+
你可以假设&nbsp;<code>pattern</code>&nbsp;只包含小写字母,&nbsp;<code>str</code>&nbsp;包含了由单个空格分隔的小写字母。&nbsp; &nbsp;&nbsp;</p>
31+
32+
### 解题思路
33+
34+
两个哈希,一个存储pattern对str的关系,一个存储str对pattern的关系,即:
35+
1. pMap以pattern的字符为key,str的单词为value;qMap则相反
36+
2. 遍历str字符串数组,同步比较相同索引位置的pattern字符。
37+
3. 如果遇到的pattern字符pMap中已经存在,取出哈希值,比较相同索引处的str单词,如果不同,返回false
38+
4. 如果遇到的pattern字符pMap中不存在,则为新的字符,这时候以相同索引处的str单词为key,看看qMap中有没有值,
39+
如果有,继续比较该str单词的pattern字符,如果不同,返回false
40+
5. 经过上面的两个考验,还活了下来,说明目前的模式匹配没问题,那就分别设置pMap和qMap,继续循环就好了
41+
6. 所有位置比较过了,都没问题的话,就返回true吧
42+
43+
44+
### 具体解法
45+
46+
47+
#### **Golang**
48+
```go
49+
func wordPattern(pattern string, str string) bool {
50+
51+
sArr := strings.Split(str, " ")
52+
if len(sArr) != len(pattern) {
53+
return false
54+
}
55+
pMap := make(map[byte]string)
56+
qMap := make(map[string]byte)
57+
58+
for i, v := range sArr {
59+
if _, ok := pMap[pattern[i]]; ok && pMap[pattern[i]] != v {
60+
return false
61+
} else {
62+
pMap[pattern[i]] = v
63+
}
64+
if _, ok := qMap[v]; ok && qMap[v] != pattern[i] {
65+
return false
66+
} else {
67+
qMap[v] = pattern[i]
68+
}
69+
}
70+
return true
71+
}
72+
```
73+
74+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package leetcode
2+
3+
import "strings"
4+
5+
/*
6+
* @lc app=leetcode.cn id=290 lang=golang
7+
*
8+
* [290] 单词规律
9+
*/
10+
11+
// @lc code=start
12+
func wordPattern(pattern string, str string) bool {
13+
14+
sArr := strings.Split(str, " ")
15+
if len(sArr) != len(pattern) {
16+
return false
17+
}
18+
pMap := make(map[byte]string)
19+
qMap := make(map[string]byte)
20+
21+
for i, v := range sArr {
22+
if _, ok := pMap[pattern[i]]; ok && pMap[pattern[i]] != v {
23+
return false
24+
} else {
25+
pMap[pattern[i]] = v
26+
}
27+
if _, ok := qMap[v]; ok && qMap[v] != pattern[i] {
28+
return false
29+
} else {
30+
qMap[v] = pattern[i]
31+
}
32+
}
33+
return true
34+
}
35+
36+
// @lc code=end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestWordPattern(t *testing.T) {
8+
var ret bool
9+
var str string
10+
var pattern string
11+
12+
str = "dog cat cat dog"
13+
pattern = "abba"
14+
ret = true
15+
if ret != wordPattern(pattern, str) {
16+
t.Fatalf("case fails %v\n", ret)
17+
}
18+
19+
str = "dog cat cat dog"
20+
pattern = "aaaa"
21+
ret = false
22+
if ret != wordPattern(pattern, str) {
23+
t.Fatalf("case fails %v\n", ret)
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /