Skip to content

Commit 9497430

Browse files
authored
feat: update solutions to lc problems
* No.2207.Maximize Number of Subsequences in a String * No.2300.Successful Pairs of Spells and Potions
1 parent 0417d77 commit 9497430

File tree

9 files changed

+208
-38
lines changed

9 files changed

+208
-38
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,85 @@
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```python
61-
61+
class Solution:
62+
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
63+
ans = 0
64+
cnt = Counter()
65+
for c in text:
66+
if c == pattern[1]:
67+
ans += cnt[pattern[0]]
68+
cnt[c] += 1
69+
ans += max(cnt[pattern[0]], cnt[pattern[1]])
70+
return ans
6271
```
6372

6473
### **Java**
6574

6675
<!-- 这里可写当前语言的特殊实现逻辑 -->
6776

6877
```java
78+
class Solution {
79+
public long maximumSubsequenceCount(String text, String pattern) {
80+
int[] cnt = new int[26];
81+
char a = pattern.charAt(0);
82+
char b = pattern.charAt(1);
83+
long ans = 0;
84+
for (char c : text.toCharArray()) {
85+
if (c == b) {
86+
ans += cnt[a - 'a'];
87+
}
88+
cnt[c - 'a']++;
89+
}
90+
ans += Math.max(cnt[a - 'a'], cnt[b - 'a']);
91+
return ans;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
long long maximumSubsequenceCount(string text, string pattern) {
102+
long long ans = 0;
103+
char a = pattern[0], b = pattern[1];
104+
vector<int> cnt(26);
105+
for (char& c : text)
106+
{
107+
if (c == b) ans += cnt[a - 'a'];
108+
cnt[c - 'a']++;
109+
}
110+
ans += max(cnt[a - 'a'], cnt[b - 'a']);
111+
return ans;
112+
}
113+
};
114+
```
69115
116+
### **Go**
117+
118+
```go
119+
func maximumSubsequenceCount(text string, pattern string) int64 {
120+
ans := 0
121+
cnt := make([]int, 26)
122+
a, b := pattern[0], pattern[1]
123+
for i := range text {
124+
c := text[i]
125+
if c == b {
126+
ans += cnt[a-'a']
127+
}
128+
cnt[c-'a']++
129+
}
130+
ans += max(cnt[a-'a'], cnt[b-'a'])
131+
return int64(ans)
132+
}
133+
134+
func max(a, b int) int {
135+
if a > b {
136+
return a
137+
}
138+
return b
139+
}
70140
```
71141

72142
### **TypeScript**

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,83 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
55+
ans = 0
56+
cnt = Counter()
57+
for c in text:
58+
if c == pattern[1]:
59+
ans += cnt[pattern[0]]
60+
cnt[c] += 1
61+
ans += max(cnt[pattern[0]], cnt[pattern[1]])
62+
return ans
5463
```
5564

5665
### **Java**
5766

5867
```java
68+
class Solution {
69+
public long maximumSubsequenceCount(String text, String pattern) {
70+
int[] cnt = new int[26];
71+
char a = pattern.charAt(0);
72+
char b = pattern.charAt(1);
73+
long ans = 0;
74+
for (char c : text.toCharArray()) {
75+
if (c == b) {
76+
ans += cnt[a - 'a'];
77+
}
78+
cnt[c - 'a']++;
79+
}
80+
ans += Math.max(cnt[a - 'a'], cnt[b - 'a']);
81+
return ans;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
long long maximumSubsequenceCount(string text, string pattern) {
92+
long long ans = 0;
93+
char a = pattern[0], b = pattern[1];
94+
vector<int> cnt(26);
95+
for (char& c : text)
96+
{
97+
if (c == b) ans += cnt[a - 'a'];
98+
cnt[c - 'a']++;
99+
}
100+
ans += max(cnt[a - 'a'], cnt[b - 'a']);
101+
return ans;
102+
}
103+
};
104+
```
59105
106+
### **Go**
107+
108+
```go
109+
func maximumSubsequenceCount(text string, pattern string) int64 {
110+
ans := 0
111+
cnt := make([]int, 26)
112+
a, b := pattern[0], pattern[1]
113+
for i := range text {
114+
c := text[i]
115+
if c == b {
116+
ans += cnt[a-'a']
117+
}
118+
cnt[c-'a']++
119+
}
120+
ans += max(cnt[a-'a'], cnt[b-'a'])
121+
return int64(ans)
122+
}
123+
124+
func max(a, b int) int {
125+
if a > b {
126+
return a
127+
}
128+
return b
129+
}
60130
```
61131

62132
### **TypeScript**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
long long maximumSubsequenceCount(string text, string pattern) {
4+
long long ans = 0;
5+
char a = pattern[0], b = pattern[1];
6+
vector<int> cnt(26);
7+
for (char& c : text)
8+
{
9+
if (c == b) ans += cnt[a - 'a'];
10+
cnt[c - 'a']++;
11+
}
12+
ans += max(cnt[a - 'a'], cnt[b - 'a']);
13+
return ans;
14+
}
15+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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']
9+
}
10+
cnt[c-'a']++
11+
}
12+
ans += max(cnt[a-'a'], cnt[b-'a'])
13+
return int64(ans)
14+
}
15+
16+
func max(a, b int) int {
17+
if a > b {
18+
return a
19+
}
20+
return b
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
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);
6+
long ans = 0;
7+
for (char c : text.toCharArray()) {
8+
if (c == b) {
9+
ans += cnt[a - 'a'];
10+
}
11+
cnt[c - 'a']++;
12+
}
13+
ans += Math.max(cnt[a - 'a'], cnt[b - 'a']);
14+
return ans;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
3+
ans = 0
4+
cnt = Counter()
5+
for c in text:
6+
if c == pattern[1]:
7+
ans += cnt[pattern[0]]
8+
cnt[c] += 1
9+
ans += max(cnt[pattern[0]], cnt[pattern[1]])
10+
return ans

solution/2300-2399/2300.Successful Pairs of Spells and Potions/README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,7 @@ class Solution:
6666
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
6767
potions.sort()
6868
m = len(potions)
69-
ans = []
70-
for s in spells:
71-
left, right = 0, m
72-
while left < right:
73-
mid = (left + right) >> 1
74-
if s * potions[mid] >= success:
75-
right = mid
76-
else:
77-
left = mid + 1
78-
ans.append(m - left)
79-
return ans
69+
return [m - bisect_left(potions, success, key=lambda x: s * x) for s in spells]
8070
```
8171

8272
### **Java**

solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,7 @@ class Solution:
5858
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
5959
potions.sort()
6060
m = len(potions)
61-
ans = []
62-
for s in spells:
63-
left, right = 0, m
64-
while left < right:
65-
mid = (left + right) >> 1
66-
if s * potions[mid] >= success:
67-
right = mid
68-
else:
69-
left = mid + 1
70-
ans.append(m - left)
71-
return ans
61+
return [m - bisect_left(potions, success, key=lambda x: s * x) for s in spells]
7262
```
7363

7464
### **Java**
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
class Solution:
2-
def successfulPairs(
3-
self, spells: List[int], potions: List[int], success: int
4-
) -> List[int]:
2+
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
53
potions.sort()
64
m = len(potions)
7-
ans = []
8-
for s in spells:
9-
left, right = 0, m
10-
while left < right:
11-
mid = (left + right) >> 1
12-
if s * potions[mid] >= success:
13-
right = mid
14-
else:
15-
left = mid + 1
16-
ans.append(m - left)
17-
return ans
5+
return [m - bisect_left(potions, success, key=lambda x: s * x) for s in spells]

0 commit comments

Comments
 (0)