Skip to content

Commit

Permalink
Add solution and test-cases for problem 2947
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Oct 20, 2024
1 parent ffaca5d commit 52d2a45
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 25 deletions.
53 changes: 40 additions & 13 deletions leetcode/2901-3000/2947.Count-Beautiful-Substrings-I/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
# [2947.Count Beautiful Substrings I][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` and a positive integer `k`.

Let `vowels` and `consonants` be the number of vowels and consonants in a string.

A string is **beautiful** if:

- `vowels == consonants`.
- `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowesl` and `consonants` is divisible by `k`.

Return the number of **non-empty beautiful substrings** in the given string `s`.

A **substrings** is a contiguous sequence of characters in a string.

**Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

**Consonant letters** in English are every letter except vowels.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "baeyh", k = 2
Output: 2
Explanation: There are 2 beautiful substrings in the given string.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
It can be shown that there are only 2 beautiful substrings in the given string.
```

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

### 思路1
> ...
Count Beautiful Substrings I
```go
```
Input: s = "abba", k = 1
Output: 3
Explanation: There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
It can be shown that there are only 3 beautiful substrings in the given string.
```

**Example 3:**

```
Input: s = "bcdf", k = 1
Output: 0
Explanation: There are no beautiful substrings in the given string.
```

## 结语

Expand Down
24 changes: 22 additions & 2 deletions leetcode/2901-3000/2947.Count-Beautiful-Substrings-I/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string, k int) int {
var isVowel func(byte) bool
isVowel = func(b byte) bool {
return b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u'
}
ans := 0
vc := make([][2]int, len(s)+1)
for i := 1; i <= len(s); i++ {
vc[i] = [2]int{vc[i-1][0], vc[i-1][1]}
if isVowel(s[i-1]) {
vc[i][0]++
} else {
vc[i][1]++
}
for j := i - 1; j > 0; j -= 2 {
a, b := vc[i][0]-vc[j-1][0], vc[i][1]-vc[j-1][1]
if a == b && (a*b)%k == 0 {
ans++
}
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
s string
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "baeyh", 2, 2},
{"TestCase2", "abba", 1, 3},
{"TestCase3", "bcdf", 1, 0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.s, c.k)
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.s, c.k)
}
})
}
}

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

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

0 comments on commit 52d2a45

Please sign in to comment.