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.

二、题解

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();
    }
};

相关推荐

  1. LeetCode2697. Lexicographically Smallest Palindrome

    2024-01-11 21:42:02       56 阅读
  2. Leetcode269.火星词典(Hard)

    2024-01-11 21:42:02       50 阅读
  3. LeetCode2696. Minimum String Length After Removing Substrings

    2024-01-11 21:42:02       49 阅读
  4. LeetCode 2696.删除子串后的字符串最小长度:栈

    2024-01-11 21:42:02       52 阅读
  5. LeetCode2696】删除子串后的字符串最小长度

    2024-01-11 21:42:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 21:42:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 21:42:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 21:42:02       87 阅读
  4. Python语言-面向对象

    2024-01-11 21:42:02       96 阅读

热门阅读

  1. Spring中线程池ThreadPoolTaskExecutor的使用

    2024-01-11 21:42:02       55 阅读
  2. FreeRTOS学习——中断管理

    2024-01-11 21:42:02       56 阅读
  3. 记录一下使用 ossfs 将oss挂载到ecs上

    2024-01-11 21:42:02       62 阅读
  4. C# typeof 与 示例的GetType()

    2024-01-11 21:42:02       60 阅读
  5. Python私教MongoDB快速入门教程

    2024-01-11 21:42:02       47 阅读
  6. QTday4

    QTday4

    2024-01-11 21:42:02      59 阅读
  7. 计算机算法贪心算法

    2024-01-11 21:42:02       66 阅读
  8. Python常用配置文件读取方法

    2024-01-11 21:42:02       50 阅读