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.
For example:
text
= "abdcdbc", pattern
= "ac"
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.text
= "aabb", pattern
= "ab"
Some of the strings which can be obtained from text
and have 6 subsequences "ab" are "aaabb", "aaabb", and "aabbb".How would you efficiently solve this problem, considering the constraints on the input string lengths (up to 10^5)? What is the time and space complexity of your solution? Can you provide the code?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach tries every single combination of letters from the given string to see if it matches the desired subsequence. Think of trying all possible ways to pick letters, one at a time, to form the subsequence. We check each possible combination to see how many times we can form the entire subsequence.
Here's how the algorithm would work step-by-step:
def maximize_subsequence_brute_force(text, pattern):
number_of_subsequences = 0
text_length = len(text)
pattern_length = len(pattern)
# Iterate through all possible subsets of the text
for i in range(2 ** text_length):
subset = ""
for j in range(text_length):
# Check if the j-th bit is set in i
if (i >> j) & 1:
subset += text[j]
# Check if the subset matches the pattern
if subset == pattern:
number_of_subsequences += 1
return number_of_subsequences
The best way to solve this problem is to count the number of times the first character of our pattern appears before each instance of the second character. We can improve the count by strategically placing more of the first character at the beginning and more of the second character at the end of the input string.
Here's how the algorithm would work step-by-step:
def maximize_number_of_subsequences(input_string, pattern):
first_char_count = 0
subsequence_count = 0
for char in input_string:
if char == pattern[0]:
first_char_count += 1
elif char == pattern[1]:
subsequence_count += first_char_count
original_count = subsequence_count
# Calculate count if first char is added at the beginning
second_char_count = input_string.count(pattern[1])
subsequence_count_add_first =
original_count + second_char_count
# Calculate count if second char is added at the end
# Need to add the initial first_char_count
first_char_count = input_string.count(pattern[0])
subsequence_count_add_second =
original_count + first_char_count
# If the pattern consists of same characters
if pattern[0] == pattern[1]:
first_char_count = input_string.count(pattern[0])
subsequence_count_add_first = (first_char_count + 1) *
(first_char_count) // 2
subsequence_count_add_second = (first_char_count) *
(first_char_count + 1) // 2
original_count = (first_char_count) * (first_char_count - 1) // 2
subsequence_count_add_first = original_count + first_char_count
subsequence_count_add_second = original_count + first_char_count
# Return the maximum subsequence count.
return max(original_count,
subsequence_count_add_first,
subsequence_count_add_second)
Case | How to Handle |
---|---|
Empty text or pattern string | Return 0 if either text or pattern is empty, as no subsequence can be formed. |
Pattern with repeating characters, e.g., 'aa' | Count occurrences of 'a' in the modified text, where we add 'a' at the beginning and end. |
Text with long sequences of one character in the pattern | Ensure the counts do not overflow (use long if necessary) when counting such subsequences. |
Text contains all the characters of pattern but not in correct order. | The algorithm still counts valid subsequences formed by matching characters regardless of their initial order. |
Pattern contains characters not present in the text. | The count for the missing characters remains zero which leads to zero subsequence count. |
Text or pattern strings with maximum allowed length. | Check for potential integer overflows during the counting process and use long data types if needed. |
Null text or pattern string | Throw IllegalArgumentException or return -1 to signify invalid input. |
Text equals pattern. | The algorithm will correctly handle it and count one valid subsequence. |