Skip to content

Commit

Permalink
Merge pull request #1003 from 0xff-dev/1963
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1963
  • Loading branch information
6boris authored Oct 30, 2024
2 parents c632db8 + 4ba6a14 commit 4272cf7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# [1963.Minimum Number of Swaps to Make the String Balanced][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 **0-indexed** string `s` of **even** length `n`. The string consists of **exactly** `n / 2` opening brackets `'['` and `n / 2` closing brackets `']'`.

A string is called **balanced** if and only if:

- It is the empty string, or
- It can be written as `AB`, where both `A` and `B` are **balanced** strings, or
- It can be written as `[C]`, where `C` is a **balanced** string.

You may swap the brackets at **any** two indices **any** number of times.

Return the **minimum** number of swaps to make `s` **balanced**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "][]["
Output: 1
Explanation: You can make the string balanced by swapping index 0 with index 3.
The resulting string is "[[]]".
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Minimum Number of Swaps to Make the String Balanced
```go
```
Input: s = "]]][[["
Output: 2
Explanation: You can do the following to make the string balanced:
- Swap index 0 with index 4. s = "[]][][".
- Swap index 1 with index 5. s = "[[][]]".
The resulting string is "[[][]]".
```

**Example 3:**

```
Input: s = "[]"
Output: 0
Explanation: The string is already balanced.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(s string) int {
bs := []byte(s)
stackIndex := -1
for index := 0; index < len(s); index++ {
if s[index] == ']' {
if stackIndex != -1 && bs[stackIndex] == '[' {
stackIndex--
continue
}
}
stackIndex++
bs[stackIndex] = s[index]
}
if stackIndex == -1 {
return 0
}
// 最终就是]]]]]][[[[ 这情况
count := (stackIndex + 1) / 2
need := count / 2
if count&1 == 1 {
need++
}
return need
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "][][", 1},
{"TestCase2", "]]][[[", 2},
{"TestCase3", "[]", 0},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

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

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

0 comments on commit 4272cf7

Please sign in to comment.