Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2207
Browse files Browse the repository at this point in the history
No.2207.Maximize Number of Subsequences in a String
  • Loading branch information
yanglbme committed Sep 19, 2024
1 parent 767a71d commit 708c157
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,18 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:遍历 + 计数

我们可以使用两个变量 $x$ 和 $y$ 分别记录当前字符串中 $\textit{pattern}[0]$ 和 $\textit{pattern}[1]$ 出现的次数。

然后遍历字符串 $\textit{text}$,对于当前遍历到的字符 $c$:

- 如果 $c$ 等于 $\textit{pattern}[1]$,我们将 $y$ 加一,此时之前出现过的所有 $\textit{pattern}[0]$ 都可以和当前的 $c$ 组成一个 $\textit{pattern}$ 子序列,因此答案加上 $x$;
- 如果 $c$ 等于 $\textit{pattern}[0]$,我们将 $x$ 加一。

遍历结束后,由于我们可以插入一个字符,因此,如果我们在字符串开头加上 $\textit{pattern}[0]$,那么可以得到 $y$ 个 $\textit{pattern}$ 子序列;如果我们在字符串结尾加上 $\textit{pattern}[1]$,那么可以得到 $x$ 个 $\textit{pattern}$ 子序列。因此,我们将答案加上 $x$ 和 $y$ 中的较大值即可。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{text}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -76,13 +87,14 @@ tags:
```python
class Solution:
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
ans = 0
cnt = Counter()
ans = x = y = 0
for c in text:
if c == pattern[1]:
ans += cnt[pattern[0]]
cnt[c] += 1
ans += max(cnt[pattern[0]], cnt[pattern[1]])
y += 1
ans += x
if c == pattern[0]:
x += 1
ans += max(x, y)
return ans
```

Expand All @@ -91,17 +103,18 @@ class Solution:
```java
class Solution {
public long maximumSubsequenceCount(String text, String pattern) {
int[] cnt = new int[26];
char a = pattern.charAt(0);
char b = pattern.charAt(1);
long ans = 0;
for (char c : text.toCharArray()) {
if (c == b) {
ans += cnt[a - 'a'];
int x = 0, y = 0;
for (int i = 0; i < text.length(); ++i) {
if (text.charAt(i) == pattern.charAt(1)) {
++y;
ans += x;
}
if (text.charAt(i) == pattern.charAt(0)) {
++x;
}
cnt[c - 'a']++;
}
ans += Math.max(cnt[a - 'a'], cnt[b - 'a']);
ans += Math.max(x, y);
return ans;
}
}
Expand All @@ -114,13 +127,17 @@ class Solution {
public:
long long maximumSubsequenceCount(string text, string pattern) {
long long ans = 0;
char a = pattern[0], b = pattern[1];
vector<int> cnt(26);
int x = 0, y = 0;
for (char& c : text) {
if (c == b) ans += cnt[a - 'a'];
cnt[c - 'a']++;
if (c == pattern[1]) {
++y;
ans += x;
}
if (c == pattern[0]) {
++x;
}
}
ans += max(cnt[a - 'a'], cnt[b - 'a']);
ans += max(x, y);
return ans;
}
};
Expand All @@ -129,19 +146,39 @@ public:
#### Go
```go
func maximumSubsequenceCount(text string, pattern string) int64 {
ans := 0
cnt := make([]int, 26)
a, b := pattern[0], pattern[1]
for i := range text {
c := text[i]
if c == b {
ans += cnt[a-'a']
func maximumSubsequenceCount(text string, pattern string) (ans int64) {
x, y := 0, 0
for _, c := range text {
if byte(c) == pattern[1] {
y++
ans += int64(x)
}
if byte(c) == pattern[0] {
x++
}
cnt[c-'a']++
}
ans += max(cnt[a-'a'], cnt[b-'a'])
return int64(ans)
ans += int64(max(x, y))
return
}
```

#### TypeScript

```ts
function maximumSubsequenceCount(text: string, pattern: string): number {
let ans = 0;
let [x, y] = [0, 0];
for (const c of text) {
if (c === pattern[1]) {
++y;
ans += x;
}
if (c === pattern[0]) {
++x;
}
}
ans += Math.max(x, y);
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo

<!-- solution:start -->

### Solution 1
### Solution 1: Traversal + Counting

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.

Then, traverse the string $\textit{text}$. For the current character $c$:

- 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.
- If $c$ equals $\textit{pattern}[0]$, increment $x$ by one.

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.

The time complexity is $O(n)$, where $n$ is the length of the string $\textit{text}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -74,13 +85,14 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo
```python
class Solution:
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
ans = 0
cnt = Counter()
ans = x = y = 0
for c in text:
if c == pattern[1]:
ans += cnt[pattern[0]]
cnt[c] += 1
ans += max(cnt[pattern[0]], cnt[pattern[1]])
y += 1
ans += x
if c == pattern[0]:
x += 1
ans += max(x, y)
return ans
```

Expand All @@ -89,17 +101,18 @@ class Solution:
```java
class Solution {
public long maximumSubsequenceCount(String text, String pattern) {
int[] cnt = new int[26];
char a = pattern.charAt(0);
char b = pattern.charAt(1);
long ans = 0;
for (char c : text.toCharArray()) {
if (c == b) {
ans += cnt[a - 'a'];
int x = 0, y = 0;
for (int i = 0; i < text.length(); ++i) {
if (text.charAt(i) == pattern.charAt(1)) {
++y;
ans += x;
}
if (text.charAt(i) == pattern.charAt(0)) {
++x;
}
cnt[c - 'a']++;
}
ans += Math.max(cnt[a - 'a'], cnt[b - 'a']);
ans += Math.max(x, y);
return ans;
}
}
Expand All @@ -112,13 +125,17 @@ class Solution {
public:
long long maximumSubsequenceCount(string text, string pattern) {
long long ans = 0;
char a = pattern[0], b = pattern[1];
vector<int> cnt(26);
int x = 0, y = 0;
for (char& c : text) {
if (c == b) ans += cnt[a - 'a'];
cnt[c - 'a']++;
if (c == pattern[1]) {
++y;
ans += x;
}
if (c == pattern[0]) {
++x;
}
}
ans += max(cnt[a - 'a'], cnt[b - 'a']);
ans += max(x, y);
return ans;
}
};
Expand All @@ -127,19 +144,39 @@ public:
#### Go
```go
func maximumSubsequenceCount(text string, pattern string) int64 {
ans := 0
cnt := make([]int, 26)
a, b := pattern[0], pattern[1]
for i := range text {
c := text[i]
if c == b {
ans += cnt[a-'a']
func maximumSubsequenceCount(text string, pattern string) (ans int64) {
x, y := 0, 0
for _, c := range text {
if byte(c) == pattern[1] {
y++
ans += int64(x)
}
if byte(c) == pattern[0] {
x++
}
cnt[c-'a']++
}
ans += max(cnt[a-'a'], cnt[b-'a'])
return int64(ans)
ans += int64(max(x, y))
return
}
```

#### TypeScript

```ts
function maximumSubsequenceCount(text: string, pattern: string): number {
let ans = 0;
let [x, y] = [0, 0];
for (const c of text) {
if (c === pattern[1]) {
++y;
ans += x;
}
if (c === pattern[0]) {
++x;
}
}
ans += Math.max(x, y);
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ class Solution {
public:
long long maximumSubsequenceCount(string text, string pattern) {
long long ans = 0;
char a = pattern[0], b = pattern[1];
vector<int> cnt(26);
int x = 0, y = 0;
for (char& c : text) {
if (c == b) ans += cnt[a - 'a'];
cnt[c - 'a']++;
if (c == pattern[1]) {
++y;
ans += x;
}
if (c == pattern[0]) {
++x;
}
}
ans += max(cnt[a - 'a'], cnt[b - 'a']);
ans += max(x, y);
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
func maximumSubsequenceCount(text string, pattern string) int64 {
ans := 0
cnt := make([]int, 26)
a, b := pattern[0], pattern[1]
for i := range text {
c := text[i]
if c == b {
ans += cnt[a-'a']
func maximumSubsequenceCount(text string, pattern string) (ans int64) {
x, y := 0, 0
for _, c := range text {
if byte(c) == pattern[1] {
y++
ans += int64(x)
}
if byte(c) == pattern[0] {
x++
}
cnt[c-'a']++
}
ans += max(cnt[a-'a'], cnt[b-'a'])
return int64(ans)
}
ans += int64(max(x, y))
return
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
class Solution {
public long maximumSubsequenceCount(String text, String pattern) {
int[] cnt = new int[26];
char a = pattern.charAt(0);
char b = pattern.charAt(1);
long ans = 0;
for (char c : text.toCharArray()) {
if (c == b) {
ans += cnt[a - 'a'];
int x = 0, y = 0;
for (int i = 0; i < text.length(); ++i) {
if (text.charAt(i) == pattern.charAt(1)) {
++y;
ans += x;
}
if (text.charAt(i) == pattern.charAt(0)) {
++x;
}
cnt[c - 'a']++;
}
ans += Math.max(cnt[a - 'a'], cnt[b - 'a']);
ans += Math.max(x, y);
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Solution:
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
ans = 0
cnt = Counter()
ans = x = y = 0
for c in text:
if c == pattern[1]:
ans += cnt[pattern[0]]
cnt[c] += 1
ans += max(cnt[pattern[0]], cnt[pattern[1]])
y += 1
ans += x
if c == pattern[0]:
x += 1
ans += max(x, y)
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function maximumSubsequenceCount(text: string, pattern: string): number {
let ans = 0;
let [x, y] = [0, 0];
for (const c of text) {
if (c === pattern[1]) {
++y;
ans += x;
}
if (c === pattern[0]) {
++x;
}
}
ans += Math.max(x, y);
return ans;
}

0 comments on commit 708c157

Please sign in to comment.