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 5c20595

Browse files
committed
Merge pull request #391 from 0xff-dev/290
Add solution and test-cases for problem 290
2 parents 99ec36a + 8692df9 commit 5c20595

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

‎leetcode/201-300/0290.Word-Pattern/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [290.Word Pattern][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a `pattern` and a string `s`, find if `s` follows the same pattern.
5+
6+
Here **follow** means a full match, such that there is a bijection between a letter in `pattern` and a **non-empty** word in `s`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: pattern = "abba", s = "dog cat cat dog"
12+
Output: true
1313
```
1414

15-
## 题意
16-
> ...
15+
**Example 2:**
1716

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Word Pattern
23-
```go
17+
```
18+
Input: pattern = "abba", s = "dog cat cat fish"
19+
Output: false
2420
```
2521

22+
**Example 3:**
23+
24+
```
25+
Input: pattern = "aaaa", s = "dog cat cat dog"
26+
Output: false
27+
```
2628

2729
## 结语
2830

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(pattern string, s string) bool {
6+
words := strings.Split(s, " ")
7+
if len(words) != len(pattern) {
8+
return false
9+
}
10+
b2w := make(map[byte]string)
11+
w2b := make(map[string]byte)
12+
b, w := pattern[0], words[0]
13+
b2w[b] = w
14+
w2b[w] = b
15+
16+
for i := 1; i < len(pattern); i++ {
17+
if pattern[i] == b {
18+
if words[i] != w {
19+
return false
20+
}
21+
continue
22+
}
23+
b, w = pattern[i], words[i]
24+
v, ok := b2w[pattern[i]]
25+
v1, ok1 := w2b[words[i]]
26+
if (ok && words[i] != v) || (ok1 && pattern[i] != v1) {
27+
return false
28+
}
29+
b2w[b] = w
30+
w2b[w] = b
31+
}
32+
return true
533
}

‎leetcode/201-300/0290.Word-Pattern/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputsbool
14-
expect bool
12+
name string
13+
pattern, sstring
14+
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abba", "dog cat cat dog", true},
17+
{"TestCase2", "abba", "dog cat cat fish", false},
18+
{"TestCase3", "aaaa", "dog cat cat dog", false},
19+
{"TestCase4", "abba", "dog dog dog dog", false},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.pattern, c.s)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.pattern, c.s)
2829
}
2930
})
3031
}
3132
}
3233

33-
//压力测试
34+
//压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
//使用案列
38+
//使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
(0)

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