Skip to content

Commit

Permalink
Merge pull request #658 from 0xff-dev/1358
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1358
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 3eaf233 + c3f522a commit d88196b
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# [1358.Number of Substrings Containing All Three Characters][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
Given a string s consisting only of characters a, b and c.

Return the number of substrings containing **at least** one occurrence of all these characters a, b and c.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "abcabc"
Output: 10
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).
```

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

## 题解

### 思路1
> ...
Number of Substrings Containing All Three Characters
```go
```
Input: s = "aaacb"
Output: 3
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb".
```

**Example 3:**

```
Input: s = "abc"
Output: 1
```

## 结语

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

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

func Solution(s string) int {
need := [3][]int{}
for i := 0; i < 3; i++ {
need[i] = make([]int, 0)
}
ans := 0
for i := len(s) - 1; i >= 0; i-- {
need[s[i]-'a'] = append(need[s[i]-'a'], i)
}
for end := len(s) - 1; end > 1; end-- {
if s[end] == 'a' {
i1 := sort.Search(len(need[1]), func(i int) bool {
return need[1][i] < end
})
i2 := sort.Search(len(need[2]), func(i int) bool {
return need[2][i] < end
})
if i1 != len(need[1]) && i2 != len(need[2]) {
x := need[1][i1]
if need[2][i2] < x {
x = need[2][i2]
}
ans += x + 1
}
}
if s[end] == 'b' {
i1 := sort.Search(len(need[0]), func(i int) bool {
return need[0][i] < end
})
i2 := sort.Search(len(need[2]), func(i int) bool {
return need[2][i] < end
})
if i1 != len(need[0]) && i2 != len(need[2]) {
x := need[0][i1]
if need[2][i2] < x {
x = need[2][i2]
}
ans += x + 1
}

}
if s[end] == 'c' {
i1 := sort.Search(len(need[0]), func(i int) bool {
return need[0][i] < end
})
i2 := sort.Search(len(need[1]), func(i int) bool {
return need[1][i] < end
})
if i1 != len(need[0]) && i2 != len(need[1]) {
x := need[0][i1]
if need[1][i2] < x {
x = need[1][i2]
}
ans += x + 1
}
}
}
return ans
}

func Solution1(s string) int {
ans := 0

chars := [3]int{}
start := 0
x := len(s)
for i := 0; i < x; i++ {
chars[s[i]-'a']++
for chars[0] > 0 && chars[1] > 0 && chars[2] > 0 {
ans += x - i
chars[s[start]-'a']--
start++
}
}
return ans
}
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", "abcabc", 10},
{"TestCase2", "aaacb", 3},
{"TestCase3", "abc", 1},
}

// 开始测试
Expand All @@ -26,14 +26,19 @@ func TestSolution(t *testing.T) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
got1 := Solution(c.inputs)
if !reflect.DeepEqual(got1, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
})
}
}

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

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

0 comments on commit d88196b

Please sign in to comment.