Skip to content

Commit 6ba88a9

Browse files
authored
feat: add solutions to lc problems: No.3083~3085 (doocs#2453)
* No.3083.Existence of a Substring in a String and Its Reverse * No.3084.Count Substrings Starting and Ending with Given Character * No.3085.Minimum Deletions to Make String K-Special
1 parent 7e1a5ca commit 6ba88a9

File tree

21 files changed

+672
-25
lines changed

21 files changed

+672
-25
lines changed

solution/3000-3099/3083.Existence of a Substring in a String and Its Reverse/README.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,88 @@
5555

5656
## 解法
5757

58-
### 方法一
58+
### 方法一:哈希表或数组
59+
60+
我们可以用一个哈希表或者二维数组 $st$ 来存储字符串 $s$ 反转后的所有长度为 $2$ 的子字符串。
61+
62+
然后我们遍历字符串 $s$,对于每一个长度为 $2$ 的子字符串,我们判断它是否在 $st$ 中出现过,是则返回 `true`。否则,遍历结束后返回 `false`
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|^2)$。其中 $n$ 为字符串 $s$ 的长度;而 $\Sigma$ 为字符串 $s$ 的字符集,本题中 $\Sigma$ 为小写英文字母,所以 $|\Sigma| = 26$。
5965

6066
<!-- tabs:start -->
6167

6268
```python
63-
69+
class Solution:
70+
def isSubstringPresent(self, s: str) -> bool:
71+
st = {(a, b) for a, b in pairwise(s[::-1])}
72+
return any((a, b) in st for a, b in pairwise(s))
6473
```
6574

6675
```java
67-
76+
class Solution {
77+
public boolean isSubstringPresent(String s) {
78+
boolean[][] st = new boolean[26][26];
79+
int n = s.length();
80+
for (int i = 0; i < n - 1; ++i) {
81+
st[s.charAt(i + 1) - 'a'][s.charAt(i) - 'a'] = true;
82+
}
83+
for (int i = 0; i < n - 1; ++i) {
84+
if (st[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a']) {
85+
return true;
86+
}
87+
}
88+
return false;
89+
}
90+
}
6891
```
6992

7093
```cpp
71-
94+
class Solution {
95+
public:
96+
bool isSubstringPresent(string s) {
97+
bool st[26][26]{};
98+
int n = s.size();
99+
for (int i = 0; i < n - 1; ++i) {
100+
st[s[i + 1] - 'a'][s[i] - 'a'] = true;
101+
}
102+
for (int i = 0; i < n - 1; ++i) {
103+
if (st[s[i] - 'a'][s[i + 1] - 'a']) {
104+
return true;
105+
}
106+
}
107+
return false;
108+
}
109+
};
72110
```
73111
74112
```go
113+
func isSubstringPresent(s string) bool {
114+
st := [26][26]bool{}
115+
for i := 0; i < len(s)-1; i++ {
116+
st[s[i+1]-'a'][s[i]-'a'] = true
117+
}
118+
for i := 0; i < len(s)-1; i++ {
119+
if st[s[i]-'a'][s[i+1]-'a'] {
120+
return true
121+
}
122+
}
123+
return false
124+
}
125+
```
75126

127+
```ts
128+
function isSubstringPresent(s: string): boolean {
129+
const st: boolean[][] = Array.from({ length: 26 }, () => Array(26).fill(false));
130+
for (let i = 0; i < s.length - 1; ++i) {
131+
st[s.charCodeAt(i + 1) - 97][s.charCodeAt(i) - 97] = true;
132+
}
133+
for (let i = 0; i < s.length - 1; ++i) {
134+
if (st[s.charCodeAt(i) - 97][s.charCodeAt(i + 1) - 97]) {
135+
return true;
136+
}
137+
}
138+
return false;
139+
}
76140
```
77141

78142
<!-- tabs:end -->

solution/3000-3099/3083.Existence of a Substring in a String and Its Reverse/README_EN.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,88 @@
5151

5252
## Solutions
5353

54-
### Solution 1
54+
### Solution 1: Hash Table or Array
55+
56+
We can use a hash table or a two-dimensional array $st$ to store all substrings of length $2$ of the reversed string $s$.
57+
58+
Then we traverse the string $s$. For each substring of length $2$, we check whether it has appeared in $st$. If it has, we return `true`. Otherwise, we return `false` after the traversal.
59+
60+
The time complexity is $O(n)$ and the space complexity is $O(|\Sigma|^2)$. Here, $n$ is the length of the string $s$, and $\Sigma$ is the character set of the string $s$. In this problem, $\Sigma$ consists of lowercase English letters, so $|\Sigma| = 26$.
5561

5662
<!-- tabs:start -->
5763

5864
```python
59-
65+
class Solution:
66+
def isSubstringPresent(self, s: str) -> bool:
67+
st = {(a, b) for a, b in pairwise(s[::-1])}
68+
return any((a, b) in st for a, b in pairwise(s))
6069
```
6170

6271
```java
63-
72+
class Solution {
73+
public boolean isSubstringPresent(String s) {
74+
boolean[][] st = new boolean[26][26];
75+
int n = s.length();
76+
for (int i = 0; i < n - 1; ++i) {
77+
st[s.charAt(i + 1) - 'a'][s.charAt(i) - 'a'] = true;
78+
}
79+
for (int i = 0; i < n - 1; ++i) {
80+
if (st[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a']) {
81+
return true;
82+
}
83+
}
84+
return false;
85+
}
86+
}
6487
```
6588

6689
```cpp
67-
90+
class Solution {
91+
public:
92+
bool isSubstringPresent(string s) {
93+
bool st[26][26]{};
94+
int n = s.size();
95+
for (int i = 0; i < n - 1; ++i) {
96+
st[s[i + 1] - 'a'][s[i] - 'a'] = true;
97+
}
98+
for (int i = 0; i < n - 1; ++i) {
99+
if (st[s[i] - 'a'][s[i + 1] - 'a']) {
100+
return true;
101+
}
102+
}
103+
return false;
104+
}
105+
};
68106
```
69107
70108
```go
109+
func isSubstringPresent(s string) bool {
110+
st := [26][26]bool{}
111+
for i := 0; i < len(s)-1; i++ {
112+
st[s[i+1]-'a'][s[i]-'a'] = true
113+
}
114+
for i := 0; i < len(s)-1; i++ {
115+
if st[s[i]-'a'][s[i+1]-'a'] {
116+
return true
117+
}
118+
}
119+
return false
120+
}
121+
```
71122

123+
```ts
124+
function isSubstringPresent(s: string): boolean {
125+
const st: boolean[][] = Array.from({ length: 26 }, () => Array(26).fill(false));
126+
for (let i = 0; i < s.length - 1; ++i) {
127+
st[s.charCodeAt(i + 1) - 97][s.charCodeAt(i) - 97] = true;
128+
}
129+
for (let i = 0; i < s.length - 1; ++i) {
130+
if (st[s.charCodeAt(i) - 97][s.charCodeAt(i + 1) - 97]) {
131+
return true;
132+
}
133+
}
134+
return false;
135+
}
72136
```
73137

74138
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool isSubstringPresent(string s) {
4+
bool st[26][26]{};
5+
int n = s.size();
6+
for (int i = 0; i < n - 1; ++i) {
7+
st[s[i + 1] - 'a'][s[i] - 'a'] = true;
8+
}
9+
for (int i = 0; i < n - 1; ++i) {
10+
if (st[s[i] - 'a'][s[i + 1] - 'a']) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func isSubstringPresent(s string) bool {
2+
st := [26][26]bool{}
3+
for i := 0; i < len(s)-1; i++ {
4+
st[s[i+1]-'a'][s[i]-'a'] = true
5+
}
6+
for i := 0; i < len(s)-1; i++ {
7+
if st[s[i]-'a'][s[i+1]-'a'] {
8+
return true
9+
}
10+
}
11+
return false
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean isSubstringPresent(String s) {
3+
boolean[][] st = new boolean[26][26];
4+
int n = s.length();
5+
for (int i = 0; i < n - 1; ++i) {
6+
st[s.charAt(i + 1) - 'a'][s.charAt(i) - 'a'] = true;
7+
}
8+
for (int i = 0; i < n - 1; ++i) {
9+
if (st[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a']) {
10+
return true;
11+
}
12+
}
13+
return false;
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def isSubstringPresent(self, s: str) -> bool:
3+
st = {(a, b) for a, b in pairwise(s[::-1])}
4+
return any((a, b) in st for a, b in pairwise(s))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function isSubstringPresent(s: string): boolean {
2+
const st: boolean[][] = Array.from({ length: 26 }, () => Array(26).fill(false));
3+
for (let i = 0; i < s.length - 1; ++i) {
4+
st[s.charCodeAt(i + 1) - 97][s.charCodeAt(i) - 97] = true;
5+
}
6+
for (let i = 0; i < s.length - 1; ++i) {
7+
if (st[s.charCodeAt(i) - 97][s.charCodeAt(i + 1) - 97]) {
8+
return true;
9+
}
10+
}
11+
return false;
12+
}

solution/3000-3099/3084.Count Substrings Starting and Ending with Given Character/README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,56 @@
4343

4444
## 解法
4545

46-
### 方法一
46+
### 方法一:数学
47+
48+
我们可以先统计字符串 $s$ 中字符 $c$ 的个数,记为 $cnt$。
49+
50+
每个 $c$ 字符可以单独作为一个子字符串,所以有 $cnt$ 个子字符串满足条件。每个 $c$ 字符可以和其他 $c$ 字符组成一个满足条件的子字符串,所以有 $\frac{cnt \times (cnt - 1)}{2}$ 个子字符串满足条件。
51+
52+
所以答案为 $cnt + \frac{cnt \times (cnt - 1)}{2}$。
53+
54+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
4755

4856
<!-- tabs:start -->
4957

5058
```python
51-
59+
class Solution:
60+
def countSubstrings(self, s: str, c: str) -> int:
61+
cnt = s.count(c)
62+
return cnt + cnt * (cnt - 1) // 2
5263
```
5364

5465
```java
55-
66+
class Solution {
67+
public long countSubstrings(String s, char c) {
68+
long cnt = s.chars().filter(ch -> ch == c).count();
69+
return cnt + cnt * (cnt - 1) / 2;
70+
}
71+
}
5672
```
5773

5874
```cpp
59-
75+
class Solution {
76+
public:
77+
long long countSubstrings(string s, char c) {
78+
long long cnt = ranges::count(s, c);
79+
return cnt + cnt * (cnt - 1) / 2;
80+
}
81+
};
6082
```
6183
6284
```go
85+
func countSubstrings(s string, c byte) int64 {
86+
cnt := int64(strings.Count(s, string(c)))
87+
return cnt + cnt*(cnt-1)/2
88+
}
89+
```
6390

91+
```ts
92+
function countSubstrings(s: string, c: string): number {
93+
const cnt = s.split('').filter(ch => ch === c).length;
94+
return cnt + Math.floor((cnt * (cnt - 1)) / 2);
95+
}
6496
```
6597

6698
<!-- tabs:end -->

solution/3000-3099/3084.Count Substrings Starting and Ending with Given Character/README_EN.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,56 @@
3939

4040
## Solutions
4141

42-
### Solution 1
42+
### Solution 1: Mathematics
43+
44+
First, we can count the number of character $c$ in string $s$, denoted as $cnt$.
45+
46+
Each character $c$ can form a substring on its own, so there are $cnt$ substrings that meet the condition. Each character $c$ can form a substring with other $c$ characters, so there are $\frac{cnt \times (cnt - 1)}{2}$ substrings that meet the condition.
47+
48+
Therefore, the answer is $cnt + \frac{cnt \times (cnt - 1)}{2}$.
49+
50+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
4351

4452
<!-- tabs:start -->
4553

4654
```python
47-
55+
class Solution:
56+
def countSubstrings(self, s: str, c: str) -> int:
57+
cnt = s.count(c)
58+
return cnt + cnt * (cnt - 1) // 2
4859
```
4960

5061
```java
51-
62+
class Solution {
63+
public long countSubstrings(String s, char c) {
64+
long cnt = s.chars().filter(ch -> ch == c).count();
65+
return cnt + cnt * (cnt - 1) / 2;
66+
}
67+
}
5268
```
5369

5470
```cpp
55-
71+
class Solution {
72+
public:
73+
long long countSubstrings(string s, char c) {
74+
long long cnt = ranges::count(s, c);
75+
return cnt + cnt * (cnt - 1) / 2;
76+
}
77+
};
5678
```
5779
5880
```go
81+
func countSubstrings(s string, c byte) int64 {
82+
cnt := int64(strings.Count(s, string(c)))
83+
return cnt + cnt*(cnt-1)/2
84+
}
85+
```
5986

87+
```ts
88+
function countSubstrings(s: string, c: string): number {
89+
const cnt = s.split('').filter(ch => ch === c).length;
90+
return cnt + Math.floor((cnt * (cnt - 1)) / 2);
91+
}
6092
```
6193

6294
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
long long countSubstrings(string s, char c) {
4+
long long cnt = ranges::count(s, c);
5+
return cnt + cnt * (cnt - 1) / 2;
6+
}
7+
};

0 commit comments

Comments
 (0)