LeetCode2696. Minimum String Length After Removing Substrings

这篇具有很好参考价值的文章主要介绍了LeetCode2696. Minimum String Length After Removing Substrings。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、题目

You are given a string s consisting only of uppercase English letters.

You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings “AB” or “CD” from s.

Return the minimum possible length of the resulting string that you can obtain.

Note that the string concatenates after removing the substring and could produce new “AB” or “CD” substrings.

Example 1:

Input: s = “ABFCACDB”
Output: 2
Explanation: We can do the following operations:

  • Remove the substring “ABFCACDB”, so s = “FCACDB”.
  • Remove the substring “FCACDB”, so s = “FCAB”.
  • Remove the substring “FCAB”, so s = “FC”.
    So the resulting length of the string is 2.
    It can be shown that it is the minimum length that we can obtain.
    Example 2:

Input: s = “ACBBD”
Output: 5
Explanation: We cannot do any operations on the string so the length remains the same.

Constraints:

1 <= s.length <= 100
s consists only of uppercase English letters.文章来源地址https://www.toymoban.com/news/detail-823726.html

二、题解

class Solution {
public:
    bool existed(string s){
        if(s.size() < 2) return false;
        int n = s.size();
        for(int i = 0;i < n - 1;i++){
            if(s[i] == 'A' && s[i + 1] == 'B') return true;
            if(s[i] == 'C' && s[i + 1] == 'D') return true;
        }
        return false;
    }
    int minLength(string s) {
        while(existed(s)){
            for(int i = 0;i < s.size() - 1;i++){
                if(s[i] == 'A' && s[i + 1] == 'B'){
                    s.erase(s.begin() + i);
                    s.erase(s.begin() + i);
                    i--;
                }
                else if(s[i] == 'C' && s[i + 1] == 'D'){
                    s.erase(s.begin() + i);
                    s.erase(s.begin() + i);
                    i--;
                }
                if(s.size() == 0) return 0;
            }
        }
        return s.size();
    }
};

到了这里,关于LeetCode2696. Minimum String Length After Removing Substrings的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode452. Minimum Number of Arrows to Burst Balloons

    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons. Arrows can be shot up directly vertically (in

    2024年02月04日
    浏览(28)
  • LeetCode //C - 153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n

    2024年02月06日
    浏览(27)
  • LeetCode每日一题(2457. Minimum Addition to Make Integer Beautiful)

    You are given two positive integers n and target. An integer is considered beautiful if the sum of its digits is less than or equal to target. Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful. Example 1: Input: n = 16, target = 6 Output: 4 Explanation: Init

    2023年04月16日
    浏览(84)
  • Leetcode 3016. Minimum Number of Pushes to Type Word II

    Leetcode 3016. Minimum Number of Pushes to Type Word II 1. 解题思路 2. 代码实现 题目链接:3016. Minimum Number of Pushes to Type Word II 这道题的话思路其实还是蛮简单的,显然我们的目的是要令对给定的word在键盘上敲击的次数最小。 因此,我们只需要对单词当中按照字符的频次进行倒序排列,

    2024年01月22日
    浏览(38)
  • LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons

    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [ x s t a r t , x e n d x_{start}, x_{end} x s t a r t ​ , x e n d ​ ] denotes a balloon whose horizontal diameter stretches between x s t a r t x_{start} x s t a r t ​ and x e n d x_{end} x

    2024年02月12日
    浏览(32)
  • LeetCode 1000. Minimum Cost to Merge Stones【记忆化搜索,动态规划,数组】困难

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月26日
    浏览(88)
  • LeetCode646. Maximum Length of Pair Chain——动态规划

    You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti righti. A pair p2 = [c, d] follows a pair p1 = [a, b] if b c. A chain of pairs can be formed in this fashion. Return the length longest chain which can be formed. You do not need to use up all the given intervals. You can select pairs in any order. Example 1: Input: pairs = [[

    2024年02月22日
    浏览(30)
  • leetcode - 1326. Minimum Number of Taps to Open to Water a Garden

    There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, …, n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i

    2024年02月10日
    浏览(28)
  • LeetCode2111. Minimum Operations to Make the Array K-Increasing——动态规划

    You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k. The array arr is called K-increasing if arr[i-k] = arr[i] holds for every index i, where k = i = n-1. For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because: arr[0] = arr[2] (4 = 5) arr[1] = arr[3] (1 = 2) arr[2] = arr[4] (5 = 6) arr[3] = arr[5]

    2024年03月09日
    浏览(33)
  • 【LeetCode 算法】Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数-Greedy

    给你一个正整数数组 nums 。每一次操作中,你可以从 nums 中选择 任意 一个数并将它 减小 到 恰好 一半 。(注意,在后续操作中你可以对减半过的数继续执行操作) 请你返回将 nums 数组和 至少 减少一半 的 最少 操作数。 1 = n u m s . l e n g t h = 1 0 5 1 = n u m s [ i ] = 1 0 7 1 = num

    2024年02月15日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包