Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec27a55

Browse files
feat: add solutions to lc problem: No.2207 (doocs#3536)
No.2207.Maximize Number of Subsequences in a String
1 parent 767a71d commit ec27a55

File tree

7 files changed

+187
-92
lines changed

7 files changed

+187
-92
lines changed

‎solution/2200-2299/2207.Maximize Number of Subsequences in a String/README.md‎

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,18 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:遍历 + 计数
71+
72+
我们可以使用两个变量 $x$ 和 $y$ 分别记录当前字符串中 $\textit{pattern}[0]$ 和 $\textit{pattern}[1]$ 出现的次数。
73+
74+
然后遍历字符串 $\textit{text},ドル对于当前遍历到的字符 $c$:
75+
76+
- 如果 $c$ 等于 $\textit{pattern}[1],ドル我们将 $y$ 加一,此时之前出现过的所有 $\textit{pattern}[0]$ 都可以和当前的 $c$ 组成一个 $\textit{pattern}$ 子序列,因此答案加上 $x$;
77+
- 如果 $c$ 等于 $\textit{pattern}[0],ドル我们将 $x$ 加一。
78+
79+
遍历结束后,由于我们可以插入一个字符,因此,如果我们在字符串开头加上 $\textit{pattern}[0],ドル那么可以得到 $y$ 个 $\textit{pattern}$ 子序列;如果我们在字符串结尾加上 $\textit{pattern}[1],ドル那么可以得到 $x$ 个 $\textit{pattern}$ 子序列。因此,我们将答案加上 $x$ 和 $y$ 中的较大值即可。
80+
81+
时间复杂度 $O(n),ドル其中 $n$ 为字符串 $\textit{text}$ 的长度。空间复杂度 $O(1)$。
7182

7283
<!-- tabs:start -->
7384

@@ -76,13 +87,14 @@ tags:
7687
```python
7788
class Solution:
7889
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
79-
ans = 0
80-
cnt = Counter()
90+
ans = x = y = 0
8191
for c in text:
8292
if c == pattern[1]:
83-
ans += cnt[pattern[0]]
84-
cnt[c] += 1
85-
ans += max(cnt[pattern[0]], cnt[pattern[1]])
93+
y += 1
94+
ans += x
95+
if c == pattern[0]:
96+
x += 1
97+
ans += max(x, y)
8698
return ans
8799
```
88100

@@ -91,17 +103,18 @@ class Solution:
91103
```java
92104
class Solution {
93105
public long maximumSubsequenceCount(String text, String pattern) {
94-
int[] cnt = new int[26];
95-
char a = pattern.charAt(0);
96-
char b = pattern.charAt(1);
97106
long ans = 0;
98-
for (char c : text.toCharArray()) {
99-
if (c == b) {
100-
ans += cnt[a - 'a'];
107+
int x = 0, y = 0;
108+
for (int i = 0; i < text.length(); ++i) {
109+
if (text.charAt(i) == pattern.charAt(1)) {
110+
++y;
111+
ans += x;
112+
}
113+
if (text.charAt(i) == pattern.charAt(0)) {
114+
++x;
101115
}
102-
cnt[c - 'a']++;
103116
}
104-
ans += Math.max(cnt[a -'a'], cnt[b -'a']);
117+
ans += Math.max(x, y);
105118
return ans;
106119
}
107120
}
@@ -114,13 +127,17 @@ class Solution {
114127
public:
115128
long long maximumSubsequenceCount(string text, string pattern) {
116129
long long ans = 0;
117-
char a = pattern[0], b = pattern[1];
118-
vector<int> cnt(26);
130+
int x = 0, y = 0;
119131
for (char& c : text) {
120-
if (c == b) ans += cnt[a - 'a'];
121-
cnt[c - 'a']++;
132+
if (c == pattern[1]) {
133+
++y;
134+
ans += x;
135+
}
136+
if (c == pattern[0]) {
137+
++x;
138+
}
122139
}
123-
ans += max(cnt[a - 'a'], cnt[b - 'a']);
140+
ans += max(x, y);
124141
return ans;
125142
}
126143
};
@@ -129,19 +146,39 @@ public:
129146
#### Go
130147
131148
```go
132-
func maximumSubsequenceCount(text string, pattern string) int64 {
133-
ans := 0
134-
cnt := make([]int, 26)
135-
a, b := pattern[0], pattern[1]
136-
for i := range text {
137-
c := text[i]
138-
if c == b {
139-
ans += cnt[a-'a']
149+
func maximumSubsequenceCount(text string, pattern string) (ans int64) {
150+
x, y := 0, 0
151+
for _, c := range text {
152+
if byte(c) == pattern[1] {
153+
y++
154+
ans += int64(x)
155+
}
156+
if byte(c) == pattern[0] {
157+
x++
140158
}
141-
cnt[c-'a']++
142159
}
143-
ans += max(cnt[a-'a'], cnt[b-'a'])
144-
return int64(ans)
160+
ans += int64(max(x, y))
161+
return
162+
}
163+
```
164+
165+
#### TypeScript
166+
167+
```ts
168+
function maximumSubsequenceCount(text: string, pattern: string): number {
169+
let ans = 0;
170+
let [x, y] = [0, 0];
171+
for (const c of text) {
172+
if (c === pattern[1]) {
173+
++y;
174+
ans += x;
175+
}
176+
if (c === pattern[0]) {
177+
++x;
178+
}
179+
}
180+
ans += Math.max(x, y);
181+
return ans;
145182
}
146183
```
147184

‎solution/2200-2299/2207.Maximize Number of Subsequences in a String/README_EN.md‎

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,18 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Traversal + Counting
69+
70+
We can use two variables $x$ and $y$ to record the current counts of $\textit{pattern}[0]$ and $\textit{pattern}[1]$ in the string, respectively.
71+
72+
Then, traverse the string $\textit{text}$. For the current character $c$:
73+
74+
- If $c$ equals $\textit{pattern}[1],ドル increment $y$ by one. At this point, all previously encountered $\textit{pattern}[0]$ can form a $\textit{pattern}$ subsequence with the current $c,ドル so add $x$ to the answer.
75+
- If $c$ equals $\textit{pattern}[0],ドル increment $x$ by one.
76+
77+
After the traversal, since we can insert one character, if we add $\textit{pattern}[0]$ at the beginning of the string, we can get $y$ $\textit{pattern}$ subsequences. If we add $\textit{pattern}[1]$ at the end of the string, we can get $x$ $\textit{pattern}$ subsequences. Therefore, we add the larger value of $x$ and $y$ to the answer.
78+
79+
The time complexity is $O(n),ドル where $n$ is the length of the string $\textit{text}$. The space complexity is $O(1)$.
6980

7081
<!-- tabs:start -->
7182

@@ -74,13 +85,14 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo
7485
```python
7586
class Solution:
7687
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
77-
ans = 0
78-
cnt = Counter()
88+
ans = x = y = 0
7989
for c in text:
8090
if c == pattern[1]:
81-
ans += cnt[pattern[0]]
82-
cnt[c] += 1
83-
ans += max(cnt[pattern[0]], cnt[pattern[1]])
91+
y += 1
92+
ans += x
93+
if c == pattern[0]:
94+
x += 1
95+
ans += max(x, y)
8496
return ans
8597
```
8698

@@ -89,17 +101,18 @@ class Solution:
89101
```java
90102
class Solution {
91103
public long maximumSubsequenceCount(String text, String pattern) {
92-
int[] cnt = new int[26];
93-
char a = pattern.charAt(0);
94-
char b = pattern.charAt(1);
95104
long ans = 0;
96-
for (char c : text.toCharArray()) {
97-
if (c == b) {
98-
ans += cnt[a - 'a'];
105+
int x = 0, y = 0;
106+
for (int i = 0; i < text.length(); ++i) {
107+
if (text.charAt(i) == pattern.charAt(1)) {
108+
++y;
109+
ans += x;
110+
}
111+
if (text.charAt(i) == pattern.charAt(0)) {
112+
++x;
99113
}
100-
cnt[c - 'a']++;
101114
}
102-
ans += Math.max(cnt[a -'a'], cnt[b -'a']);
115+
ans += Math.max(x, y);
103116
return ans;
104117
}
105118
}
@@ -112,13 +125,17 @@ class Solution {
112125
public:
113126
long long maximumSubsequenceCount(string text, string pattern) {
114127
long long ans = 0;
115-
char a = pattern[0], b = pattern[1];
116-
vector<int> cnt(26);
128+
int x = 0, y = 0;
117129
for (char& c : text) {
118-
if (c == b) ans += cnt[a - 'a'];
119-
cnt[c - 'a']++;
130+
if (c == pattern[1]) {
131+
++y;
132+
ans += x;
133+
}
134+
if (c == pattern[0]) {
135+
++x;
136+
}
120137
}
121-
ans += max(cnt[a - 'a'], cnt[b - 'a']);
138+
ans += max(x, y);
122139
return ans;
123140
}
124141
};
@@ -127,19 +144,39 @@ public:
127144
#### Go
128145
129146
```go
130-
func maximumSubsequenceCount(text string, pattern string) int64 {
131-
ans := 0
132-
cnt := make([]int, 26)
133-
a, b := pattern[0], pattern[1]
134-
for i := range text {
135-
c := text[i]
136-
if c == b {
137-
ans += cnt[a-'a']
147+
func maximumSubsequenceCount(text string, pattern string) (ans int64) {
148+
x, y := 0, 0
149+
for _, c := range text {
150+
if byte(c) == pattern[1] {
151+
y++
152+
ans += int64(x)
153+
}
154+
if byte(c) == pattern[0] {
155+
x++
138156
}
139-
cnt[c-'a']++
140157
}
141-
ans += max(cnt[a-'a'], cnt[b-'a'])
142-
return int64(ans)
158+
ans += int64(max(x, y))
159+
return
160+
}
161+
```
162+
163+
#### TypeScript
164+
165+
```ts
166+
function maximumSubsequenceCount(text: string, pattern: string): number {
167+
let ans = 0;
168+
let [x, y] = [0, 0];
169+
for (const c of text) {
170+
if (c === pattern[1]) {
171+
++y;
172+
ans += x;
173+
}
174+
if (c === pattern[0]) {
175+
++x;
176+
}
177+
}
178+
ans += Math.max(x, y);
179+
return ans;
143180
}
144181
```
145182

‎solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ class Solution {
22
public:
33
long long maximumSubsequenceCount(string text, string pattern) {
44
long long ans = 0;
5-
char a = pattern[0], b = pattern[1];
6-
vector<int> cnt(26);
5+
int x = 0, y = 0;
76
for (char& c : text) {
8-
if (c == b) ans += cnt[a - 'a'];
9-
cnt[c - 'a']++;
7+
if (c == pattern[1]) {
8+
++y;
9+
ans += x;
10+
}
11+
if (c == pattern[0]) {
12+
++x;
13+
}
1014
}
11-
ans += max(cnt[a - 'a'], cnt[b - 'a']);
15+
ans += max(x, y);
1216
return ans;
1317
}
14-
};
18+
};
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
func maximumSubsequenceCount(text string, pattern string) int64 {
2-
ans := 0
3-
cnt := make([]int, 26)
4-
a, b := pattern[0], pattern[1]
5-
for i := range text {
6-
c := text[i]
7-
if c == b {
8-
ans += cnt[a-'a']
1+
func maximumSubsequenceCount(text string, pattern string) (ans int64) {
2+
x, y := 0, 0
3+
for _, c := range text {
4+
if byte(c) == pattern[1] {
5+
y++
6+
ans += int64(x)
7+
}
8+
if byte(c) == pattern[0] {
9+
x++
910
}
10-
cnt[c-'a']++
1111
}
12-
ans += max(cnt[a-'a'], cnt[b-'a'])
13-
returnint64(ans)
14-
}
12+
ans += int64(max(x, y))
13+
return
14+
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Solution {
22
public long maximumSubsequenceCount(String text, String pattern) {
3-
int[] cnt = new int[26];
4-
char a = pattern.charAt(0);
5-
char b = pattern.charAt(1);
63
long ans = 0;
7-
for (char c : text.toCharArray()) {
8-
if (c == b) {
9-
ans += cnt[a - 'a'];
4+
int x = 0, y = 0;
5+
for (int i = 0; i < text.length(); ++i) {
6+
if (text.charAt(i) == pattern.charAt(1)) {
7+
++y;
8+
ans += x;
9+
}
10+
if (text.charAt(i) == pattern.charAt(0)) {
11+
++x;
1012
}
11-
cnt[c - 'a']++;
1213
}
13-
ans += Math.max(cnt[a - 'a'], cnt[b - 'a']);
14+
ans += Math.max(x, y);
1415
return ans;
1516
}
16-
}
17+
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution:
22
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
3-
ans = 0
4-
cnt = Counter()
3+
ans = x = y = 0
54
for c in text:
65
if c == pattern[1]:
7-
ans += cnt[pattern[0]]
8-
cnt[c] += 1
9-
ans += max(cnt[pattern[0]], cnt[pattern[1]])
6+
y += 1
7+
ans += x
8+
if c == pattern[0]:
9+
x += 1
10+
ans += max(x, y)
1011
return ans
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maximumSubsequenceCount(text: string, pattern: string): number {
2+
let ans = 0;
3+
let [x, y] = [0, 0];
4+
for (const c of text) {
5+
if (c === pattern[1]) {
6+
++y;
7+
ans += x;
8+
}
9+
if (c === pattern[0]) {
10+
++x;
11+
}
12+
}
13+
ans += Math.max(x, y);
14+
return ans;
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /