Skip to content

Add solution and test-cases for problem 1807 #1266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
# [1807.Evaluate the Bracket Pairs of a String][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a string `s` that contains some bracket pairs, with each pair containing a **non-empty** key.

- For example, in the string `"(name)is(age)yearsold"`, there are **two** bracket pairs that contain the keys `"name"` and `"age"`.

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`.

You are tasked to evaluate **all** of the bracket pairs. When you evaluate a bracket pair that contains some key `key1`, you will:

- Replace `keyi` and the bracket pair with the key's corresponding `valuei`.
- 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).

Each key will appear at most once in your `knowledge`. There will not be any nested brackets in `s`.

Return the resulting string after evaluating **all** of the bracket pairs.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]]
Output: "bobistwoyearsold"
Explanation:
The key "name" has a value of "bob", so replace "(name)" with "bob".
The key "age" has a value of "two", so replace "(age)" with "two".
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Evaluate the Bracket Pairs of a String
```go
```
Input: s = "hi(name)", knowledge = [["a","b"]]
Output: "hi?"
Explanation: As you do not know the value of the key "name", replace "(name)" with "?".
```

**Example 3:**

```
Input: s = "(a)(a)(a)aaa", knowledge = [["a","yes"]]
Output: "yesyesyesaaa"
Explanation: The same key can appear multiple times.
The key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes".
Notice that the "a"s not in a bracket pair are not evaluated.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

func Solution(s string, knowledge [][]string) string {
values := make(map[string]string)
for _, v := range knowledge {
values[v[0]] = v[1]
}

sb := strings.Builder{}
var key, replace string
left := -1
for i, b := range s {
if b == '(' {
left = i
continue
}
if b == ')' {
key = s[left+1 : i]
replace = "?"
if v, ok := values[key]; ok {
replace = v
}
left = -1
sb.WriteString(replace)
continue
}
if left == -1 {
sb.WriteByte(byte(b))
}
}
return sb.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,39 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs string
knowledge [][]string

expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "(name)is(age)yearsold", [][]string{
{"name", "bob"}, {"age", "two"},
}, "bobistwoyearsold"},
{"TestCase2", "hi(name)", [][]string{
{"a", "b"},
}, "hi?"},
{"TestCase3", "(a)(a)(a)aaa", [][]string{
{"a", "yes"},
}, "yesyesyesaaa"},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.knowledge)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.knowledge)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading