-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2207 (#3536)
No.2207.Maximize Number of Subsequences in a String
- Loading branch information
Showing
7 changed files
with
187 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 12 additions & 12 deletions
24
solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
19 changes: 10 additions & 9 deletions
19
solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
11 changes: 6 additions & 5 deletions
11
solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |