Skip to content

Commit 0f863f9

Browse files
authored
Merge pull request #982 from 0xff-dev/1684
Add solution and test-cases for problem 1684
2 parents 0b20528 + 1fe0257 commit 0f863f9

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

leetcode/1601-1700/1684.Count-the-Number-of-Consistent-Strings/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1684.Count the Number of Consistent Strings][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+
You are given a string `allowed` consisting of **distinct** characters and an array of strings `words`. A string is **consistent** if all characters in the string appear in the string `allowed`.
5+
6+
Return the number of **consistent** strings in the array words.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
12+
Output: 2
13+
Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
1314
```
1415

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

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count the Number of Consistent Strings
23-
```go
18+
```
19+
Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
20+
Output: 7
21+
Explanation: All strings are consistent.
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
28+
Output: 4
29+
Explanation: Strings "cc", "acd", "ac", and "d" are consistent.
30+
```
2631

2732
## 结语
2833

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(allowed string, words []string) int {
4+
in := [26]bool{}
5+
for _, b := range allowed {
6+
in[b-'a'] = true
7+
}
8+
ans := 0
9+
for _, word := range words {
10+
ok := true
11+
for _, b := range word {
12+
if !in[b-'a'] {
13+
ok = false
14+
break
15+
}
16+
}
17+
if ok {
18+
ans++
19+
}
20+
}
21+
return ans
522
}

leetcode/1601-1700/1684.Count-the-Number-of-Consistent-Strings/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-
inputs bool
14-
expect bool
12+
name string
13+
allowed string
14+
words []string
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "ab", []string{"ad", "bd", "aaab", "baa", "badab"}, 2},
18+
{"TestCase2", "abc", []string{"a", "b", "c", "ab", "ac", "bc", "abc"}, 7},
19+
{"TestCase3", "cad", []string{"cc", "acd", "b", "ba", "bac", "bad", "ac", "d"}, 4},
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.allowed, c.words)
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.allowed, c.words)
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)