LeetCode每日一题(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 “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 <= 105
  • pattern.length == 2
  • text and pattern consist only of lowercase English letters.

分两种情况:

  1. 如果 pattern[0] == pattern[1], 例如 pattern = “rr”, 此时的放置位置对我们没有任何影响, 这种情况我们只需要找出 text 中 pattern[0]的数量 count 然后加上 1, 套用公式 n _ (n-1) / 2, 即(count + 1) _ count / 2 就可以得出答案

  2. 如果 pattern[0] != pattern[1], 为保证最优解, 此时我们要么在 text 中所有 pattern[1]的前面加上 pattern[0], 要么在 text 中所有 pattern[0]后添加 pattern[1], 简化一下, 我们要么在 text[0]处插入 pattern[0]要么在 text 末尾 append pattern[1]。如果我们选择插入 pattern[0], 那我们最终会未答案贡献 text 中 pattern[1]的数量, 比如 text = “cabbd”, pattern = “ab”, 此时我们如果插入 a, text = “acabbd”, 因为 text 中有 2 个 b, 所以我们插入的这个 a 可以生成 2 个 ab, 同理, 如果我们选择 append pattern[1], text = “cabbdb”, text 中只有 1 个 a, 所以我们新添加的这个 b 只能生成一个 ab。两种情况比较我们肯定会选择插入 a。剩下的就跟情况 1 一样, 我们只需要计算出原有的 text 中有多少个 ab 就可以了, 然后用这个计数加上我们刚才判断出来的最大增益就是最终答案


impl Solution {
    pub fn maximum_subsequence_count(text: String, pattern: String) -> i64 {
        let first_letter = pattern.chars().nth(0).unwrap();
        let second_letter = pattern.chars().nth(1).unwrap();
        let mut first_letter_count = 0;
        let mut second_letter_count = 0;
        for c in text.chars() {
            if c == first_letter {
                first_letter_count += 1;
                continue;
            }
            if c == second_letter {
                second_letter_count += 1;
            }
        }
        if first_letter == second_letter {
            first_letter_count += 1;
            return first_letter_count * (first_letter_count - 1) / 2;
        }
        let mut slc = second_letter_count;
        let mut pairs = 0;
        for c in text.chars() {
            if c == first_letter {
                pairs += slc;
                continue;
            }
            if c == second_letter {
                slc -= 1;
            }
        }
        pairs + first_letter_count.max(second_letter_count)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值