Skip to content

Commit cdefcdf

Browse files
authored
Merge pull request #1266 from 0xff-dev/1807
Add solution and test-cases for problem 1807
2 parents 9b2c73e + 553abd6 commit cdefcdf

File tree

3 files changed

+83
-26
lines changed

3 files changed

+83
-26
lines changed

leetcode/1801-1900/1807.Evaluate-the-Bracket-Pairs-of-a-String/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [1807.Evaluate the Bracket Pairs of a String][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 `s` that contains some bracket pairs, with each pair containing a **non-empty** key.
5+
6+
- For example, in the string `"(name)is(age)yearsold"`, there are **two** bracket pairs that contain the keys `"name"` and `"age"`.
7+
8+
You know the values of a wide range of keys. This is represented by a 2D string array `knowledge` where each `knowledge[i] = [keyi, valuei]` indicates that key `keyi` has a value of `valuei`.
9+
10+
You are tasked to evaluate **all** of the bracket pairs. When you evaluate a bracket pair that contains some key `key1`, you will:
11+
12+
- Replace `keyi` and the bracket pair with the key's corresponding `valuei`.
13+
- If you do not know the value of the key, you will replace `keyi` and the bracket pair with a question mark `"?"` (without the quotation marks).
14+
15+
Each key will appear at most once in your `knowledge`. There will not be any nested brackets in `s`.
16+
17+
Return the resulting string after evaluating **all** of the bracket pairs.
718

819
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]]
23+
Output: "bobistwoyearsold"
24+
Explanation:
25+
The key "name" has a value of "bob", so replace "(name)" with "bob".
26+
The key "age" has a value of "two", so replace "(age)" with "two".
1327
```
1428

15-
## 题意
16-
> ...
17-
18-
## 题解
29+
**Example 2:**
1930

20-
### 思路1
21-
> ...
22-
Evaluate the Bracket Pairs of a String
23-
```go
31+
```
32+
Input: s = "hi(name)", knowledge = [["a","b"]]
33+
Output: "hi?"
34+
Explanation: As you do not know the value of the key "name", replace "(name)" with "?".
2435
```
2536

37+
**Example 3:**
38+
39+
```
40+
Input: s = "(a)(a)(a)aaa", knowledge = [["a","yes"]]
41+
Output: "yesyesyesaaa"
42+
Explanation: The same key can appear multiple times.
43+
The key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes".
44+
Notice that the "a"s not in a bracket pair are not evaluated.
45+
```
2646

2747
## 结语
2848

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(s string, knowledge [][]string) string {
6+
values := make(map[string]string)
7+
for _, v := range knowledge {
8+
values[v[0]] = v[1]
9+
}
10+
11+
sb := strings.Builder{}
12+
var key, replace string
13+
left := -1
14+
for i, b := range s {
15+
if b == '(' {
16+
left = i
17+
continue
18+
}
19+
if b == ')' {
20+
key = s[left+1 : i]
21+
replace = "?"
22+
if v, ok := values[key]; ok {
23+
replace = v
24+
}
25+
left = -1
26+
sb.WriteString(replace)
27+
continue
28+
}
29+
if left == -1 {
30+
sb.WriteByte(byte(b))
31+
}
32+
}
33+
return sb.String()
534
}

leetcode/1801-1900/1807.Evaluate-the-Bracket-Pairs-of-a-String/Solution_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,39 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs string
14+
knowledge [][]string
15+
16+
expect string
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", "(name)is(age)yearsold", [][]string{
19+
{"name", "bob"}, {"age", "two"},
20+
}, "bobistwoyearsold"},
21+
{"TestCase2", "hi(name)", [][]string{
22+
{"a", "b"},
23+
}, "hi?"},
24+
{"TestCase3", "(a)(a)(a)aaa", [][]string{
25+
{"a", "yes"},
26+
}, "yesyesyesaaa"},
1927
}
2028

2129
// 开始测试
2230
for i, c := range cases {
2331
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
32+
got := Solution(c.inputs, c.knowledge)
2533
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
34+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
35+
c.expect, got, c.inputs, c.knowledge)
2836
}
2937
})
3038
}
3139
}
3240

33-
// 压力测试
41+
// 压力测试
3442
func BenchmarkSolution(b *testing.B) {
3543
}
3644

37-
// 使用案列
45+
// 使用案列
3846
func ExampleSolution() {
3947
}

0 commit comments

Comments
 (0)