2207. Maximize Number of Subsequences in a String Medium
1/**
2 * [2207] Maximize Number of Subsequences in a String
3 *
4 * You are given a 0-indexed string text and another 0-indexed string pattern of length 2, both of which consist of only lowercase English letters.
5 * You can add either pattern[0] or pattern[1] anywhere in text exactly once. Note that the character can be added even at the beginning or at the end of text.
6 * Return the maximum number of times pattern can occur as a subsequence of the modified text.
7 * A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
8 *
9 * Example 1:
10 *
11 * Input: text = "abdcdbc", pattern = "ac"
12 * Output: 4
13 * Explanation:
14 * If we add pattern[0] = 'a' in between text[1] and text[2], we get "ab<u>a</u>dcdbc". Now, the number of times "ac" occurs as a subsequence is 4.
15 * Some other strings which have 4 subsequences "ac" after adding a character to text are "<u>a</u>abdcdbc" and "abd<u>a</u>cdbc".
16 * However, strings such as "abdc<u>a</u>dbc", "abd<u>c</u>cdbc", and "abdcdbc<u>c</u>", although obtainable, have only 3 subsequences "ac" and are thus suboptimal.
17 * It can be shown that it is not possible to get more than 4 subsequences "ac" by adding only one character.
18 *
19 * Example 2:
20 *
21 * Input: text = "aabb", pattern = "ab"
22 * Output: 6
23 * Explanation:
24 * Some of the strings which can be obtained from text and have 6 subsequences "ab" are "<u>a</u>aabb", "aa<u>a</u>bb", and "aab<u>b</u>b".
25 *
26 *
27 * Constraints:
28 *
29 * 1 <= text.length <= 10^5
30 * pattern.length == 2
31 * text and pattern consist only of lowercase English letters.
32 *
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/maximize-number-of-subsequences-in-a-string/
37// discuss: https://leetcode.com/problems/maximize-number-of-subsequences-in-a-string/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42 pub fn maximum_subsequence_count(text: String, pattern: String) -> i64 {
43
44 }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn test_2207() {
55 }
56}
57
Back
© 2025 bowen.ge All Rights Reserved.