Class Solution
-
- All Implemented Interfaces:
public final class Solution2207 - Maximize Number of Subsequences in a String.
Medium
You are given a 0-indexed string
textand another 0-indexed stringpatternof length2, both of which consist of only lowercase English letters.You can add either
pattern[0]orpattern[1]anywhere intextexactly once. Note that the character can be added even at the beginning or at the end oftext.Return the maximum number of times
patterncan occur as a subsequence of the modifiedtext.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 pattern0 = 'a' in between text1 and text2, 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:
<code>1 <= text.length <= 10<sup>5</sup></code>
pattern.length == 2textandpatternconsist only of lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final LongmaximumSubsequenceCount(String text, String pattern)-
-
Method Detail
-
maximumSubsequenceCount
final Long maximumSubsequenceCount(String text, String pattern)
-
-
-
-