Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Move from brute-force thinking to an efficient approach using greedy strategy.
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.
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.
Return the maximum number of times pattern can occur as a subsequence of the modified text.
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.
Example 1:
Input: text = "abdcdbc", pattern = "ac" Output: 4 Explanation: If we add pattern[0] = 'a' in between text[1] and text[2], we get "abadcdbc". Now, the number of times "ac" occurs as a subsequence is 4. Some other strings which have 4 subsequences "ac" after adding a character to text are "aabdcdbc" and "abdacdbc". However, strings such as "abdcadbc", "abdccdbc", and "abdcdbcc", although obtainable, have only 3 subsequences "ac" and are thus suboptimal. It can be shown that it is not possible to get more than 4 subsequences "ac" by adding only one character.
Example 2:
Input: text = "aabb", pattern = "ab" Output: 6 Explanation: Some of the strings which can be obtained from text and have 6 subsequences "ab" are "aaabb", "aaabb", and "aabbb".
Constraints:
1 <= text.length <= 105pattern.length == 2text and pattern consist only of lowercase English letters.Problem summary: 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. 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. Return the maximum number of times pattern can occur as a subsequence of the modified text. 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"abdcdbc" "ac"
"aabb" "ab"
longest-common-subsequence)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2207: Maximize Number of Subsequences in a String
class Solution {
public long maximumSubsequenceCount(String text, String pattern) {
long ans = 0;
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;
}
}
ans += Math.max(x, y);
return ans;
}
}
// Accepted solution for LeetCode #2207: Maximize Number of Subsequences in a String
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++
}
}
ans += int64(max(x, y))
return
}
# Accepted solution for LeetCode #2207: Maximize Number of Subsequences in a String
class Solution:
def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
ans = x = y = 0
for c in text:
if c == pattern[1]:
y += 1
ans += x
if c == pattern[0]:
x += 1
ans += max(x, y)
return ans
// Accepted solution for LeetCode #2207: Maximize Number of Subsequences in a String
/**
* [2207] Maximize Number of Subsequences in a String
*
* 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.
* 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.
* Return the maximum number of times pattern can occur as a subsequence of the modified text.
* 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.
*
* Example 1:
*
* Input: text = "abdcdbc", pattern = "ac"
* Output: 4
* Explanation:
* 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.
* 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".
* 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.
* It can be shown that it is not possible to get more than 4 subsequences "ac" by adding only one character.
*
* Example 2:
*
* Input: text = "aabb", pattern = "ab"
* Output: 6
* Explanation:
* 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".
*
*
* Constraints:
*
* 1 <= text.length <= 10^5
* pattern.length == 2
* text and pattern consist only of lowercase English letters.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximize-number-of-subsequences-in-a-string/
// discuss: https://leetcode.com/problems/maximize-number-of-subsequences-in-a-string/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_subsequence_count(text: String, pattern: String) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2207_example_1() {
let text = "abdcdbc".to_string();
let pattern = "ac".to_string();
let result = 4;
assert_eq!(Solution::maximum_subsequence_count(text, pattern), result);
}
#[test]
#[ignore]
fn test_2207_example_2() {
let text = "aabb".to_string();
let pattern = "ab".to_string();
let result = 6;
assert_eq!(Solution::maximum_subsequence_count(text, pattern), result);
}
}
// Accepted solution for LeetCode #2207: Maximize Number of Subsequences in a String
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;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.