java.lang.Object
g2201_2300.s2207_maximize_number_of_subsequences_in_a_string.Solution

public class Solution extends Object
2207 - Maximize Number of Subsequences in a String.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> string <code>text</code> and another <strong>0-indexed</strong> string <code>pattern</code> of length <code>2</code>, both of which consist of only lowercase English letters.</p> <p>You can add <strong>either</strong> <code>pattern[0]</code> <strong>or</strong> <code>pattern[1]</code> anywhere in <code>text</code> <strong>exactly once</strong>. Note that the character can be added even at the beginning or at the end of <code>text</code>.</p> <p>Return <em>the <strong>maximum</strong> number of times</em> <code>pattern</code> <em>can occur as a <strong>subsequence</strong> of the modified</em> <code>text</code>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> text = &ldquo;abdcdbc&rdquo;, pattern = &ldquo;ac&rdquo;</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>If we add pattern[0] = &lsquo;a&rsquo; in between text[1] and text[2], we get &ldquo;ab<strong>a</strong>dcdbc&rdquo;. Now, the number of times &ldquo;ac&rdquo; occurs as a subsequence is 4.</p> <p>Some other strings which have 4 subsequences &ldquo;ac&rdquo; after adding a character to text are &ldquo;<strong>a</strong>abdcdbc&rdquo; and &ldquo;abd<strong>a</strong>cdbc&rdquo;.</p> <p>However, strings such as &ldquo;abdc<strong>a</strong>dbc&rdquo;, &ldquo;abd<strong>c</strong>cdbc&rdquo;, and &ldquo;abdcdbc<strong>c</strong>&rdquo;, although obtainable, have only 3 subsequences &ldquo;ac&rdquo; and are thus suboptimal.</p> <p>It can be shown that it is not possible to get more than 4 subsequences &ldquo;ac&rdquo; by adding only one character.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> text = &ldquo;aabb&rdquo;, pattern = &ldquo;ab&rdquo;</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong></p> <p>Some of the strings which can be obtained from text and have 6 subsequences &ldquo;ab&rdquo; are &ldquo;<strong>a</strong>aabb&rdquo;, &ldquo;aa<strong>a</strong>bb&rdquo;, and &ldquo;aab<strong>b</strong>b&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= text.length <= 10<sup>5</sup></code></li> <li><code>pattern.length == 2</code></li> <li><code>text</code> and <code>pattern</code> consist only of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumSubsequenceCount

      public long maximumSubsequenceCount(String text, String pattern)