You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2200-2299/2207.Maximize Number of Subsequences in a String/README_EN.md
+67-30
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,18 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo
65
65
66
66
<!-- solution:start -->
67
67
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)$.
69
80
70
81
<!-- tabs:start -->
71
82
@@ -74,13 +85,14 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo
0 commit comments