Magicsheet logo

Maximize Number of Subsequences in a String

Medium
25%
Updated 8/1/2025

Asked by 1 Company

Maximize Number of Subsequences in a String

What is this problem about?

In the Maximize Number of Subsequences in a String problem, you are given a string text and a pattern string of length exactly 2 (e.g., "ab"). You are allowed to add exactly one character to text, and this character must be either pattern[0] or pattern[1]. You can place it anywhere in text. Your goal is to maximize the number of times pattern appears as a subsequence in text.

Why is this asked in interviews?

This is a brilliant string optimization and greedy math problem. Interviewers ask it because it forces candidates to evaluate optimal placement without brute-forcing. If you try to insert the character at every possible index and recount the subsequences (O(N2)O(N^2)), you will fail. It tests if you understand that adding to the extreme edges provides the maximum combinatorial benefit.

Algorithmic pattern used

This problem uses Prefix/Suffix Counting and Greedy Placement.

  1. To maximize subsequences, if you add pattern[0], you want it to be before as many pattern[1]s as possible. Therefore, you should optimally place it at the very beginning of the string (index 0).
  2. If you add pattern[1], you want it to be after as many pattern[0]s as possible. Therefore, you should place it at the very end of the string.
  3. You traverse the original string once. Count how many times pattern[0] and pattern[1] appear. As you traverse, if you see pattern[1], you add your current count of pattern[0] to your total subsequence tally.
  4. Finally, your optimal addition will grant you extra subsequences equal to max(count(pattern[0]), count(pattern[1])). Add this to your total.

Example explanation

text = "abdcdbc", pattern = "ac" Let's traverse the original text counting 'a' and 'c', and accumulating subsequences.

  • 'a': count_a = 1.
  • 'b', 'd': ignore.
  • 'c': We found a 'c'. The current count_a is 1. So we formed 1 subsequence. count_c = 1. Total = 1.
  • 'd', 'b': ignore.
  • 'c': Found another 'c'. count_a is still 1. We form 1 more subsequence. count_c = 2. Total = 2.

Original subsequences = 2. We can add 'a' at the start. It will pair with all existing 'c's (count_c = 2). New total = 4. Or we can add 'c' at the end. It will pair with all existing 'a's (count_a = 1). New total = 3. We take the maximum. The answer is 4.

Common mistakes candidates make

A major mistake is physically manipulating strings by doing text.insert(i, char) and writing a helper function to recount. This requires O(N)O(N) insertions and O(N)O(N) counting, yielding O(N2)O(N^2) time. Another common error occurs when pattern[0] == pattern[1] (e.g., "zz"). The logic still perfectly holds, but candidates often write specialized edge cases that accidentally double-count elements. The standard formula handles identical characters naturally if implemented precisely.

Interview preparation tip

When tackling the Maximize Number of Subsequences interview question, always remember the "Edge Insertion Rule" for subsequence matching. If you need a character to act as a prefix for as many elements as possible, it must go at index 0. If it needs to act as a suffix, it goes at the end. You never need to test middle insertions.

Similar Questions