【力扣每日一题】力扣2696删除子串后的字符串最小长度

题目来源

力扣2696删除子串后的字符串最小长度

题目概述

给你一个仅由 大写 英文字符组成的字符串 s 。 你可以对此字符串执行一些操作,在每一步操作中,你可以从 s 中删除 任一个 "AB" 或 "CD" 子字符串。 通过执行操作,删除所有 "AB" 和 "CD" 子串,返回可获得的最终字符串的 最小 可能长度。 注意,删除子串后,重新连接出的字符串可能会产生新的 "AB" 或 "CD" 子串。

示例

示例 1:

输入:s = "ABFCACDB"

输出:2

解释:你可以执行下述操作:

  • 从 "ABFCACDB" 中删除子串 "AB",得到 s = "FCACDB" 。
  • 从 "FCACDB" 中删除子串 "CD",得到 s = "FCAB" 。
  • 从 "FCAB" 中删除子串 "AB",得到 s = "FC" 。

最终字符串的长度为 2 。 可以证明 2 是可获得的最小长度。

示例 2:

输入:s = "ACBBD"

输出:5

解释:无法执行操作,字符串长度不变。

提示

  • 1 <= s.length <= 100
  • s 仅由大写英文字母组成

解题思路

  1. 使用栈来保存可能需要删除的前半部分子串;
  2. 当我们遇到A或者C时入栈,等待匹配;
  3. 当遇到B或D时,判断栈顶是否是对应的前一个字符,如果是则匹配成功,被删除子串长度+2,反之匹配失败,栈中的子串无法再被连续匹配,全部出栈;
  4. 当遇到的不是ABCD中的任何一个,栈中的子串都无法被连续匹配,全部出栈。

代码实现

java实现

public class Solution {
    public int minLength(String s) {
        int length = s.length();
        int count = 0;
        int[] arrayStack = new int[length];
        int topIndex= 0;
        for(int i = 0; i < length; i++) {
            char current = s.charAt(i);
            if (current == 'A' || current == 'C') {
                // 遇到AC入栈
                arrayStack[topIndex++] = current;
            }else if(current == 'B' || current == 'D') {
                // 栈为空,无法匹配,无视该字符
                if (topIndex == 0) {
                    continue;
                }
                // 如果B前一个是A,或者D前一个是C
                if (current - arrayStack[topIndex - 1] == 1) {
                    count += 2;
                    topIndex--;
                }else {
                    topIndex = 0;
                }
            }else {
                // 如果没有遇到ABCD出栈全部字符
                topIndex = 0;
            }
        }
        return length - count;
    }
}

c++实现

class Solution {
public:
    int minLength(string s) {
        int length = s.length();
        int count = 0;
        vector<int> arrayStack = vector<int>(length);
        int topIndex = 0;
        for (int i = 0; i < length; i++) {
            char current = s[i];
            if (current == 'A' || current == 'C') {
                // 遇到AC入栈
                arrayStack[topIndex++] = current;
            }
            else if (current == 'B' || current == 'D') {
                // 栈为空,无法匹配,无视该字符
                if (topIndex == 0) {
                    continue;
                }
                // 如果B前一个是A,或者D前一个是C
                if (current - arrayStack[topIndex - 1] == 1) {
                    count += 2;
                    topIndex--;
                }
                else {
                    topIndex = 0;
                }
            }
            else {
                // 如果没有遇到ABCD出栈全部字符
                topIndex = 0;
            }
        }
        return length - count;
    }
};

运行结果

java结果

c++结果

最近更新

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

    2024-01-11 17:42:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 17:42:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 17:42:04       82 阅读
  4. Python语言-面向对象

    2024-01-11 17:42:04       91 阅读

热门阅读

  1. kotlin的注解

    2024-01-11 17:42:04       56 阅读
  2. PyTorch知识图谱

    2024-01-11 17:42:04       55 阅读
  3. 谷歌倾斜摄影数据获取以及转换3DTiles介绍

    2024-01-11 17:42:04       75 阅读
  4. postgresql 查询字段 信息

    2024-01-11 17:42:04       54 阅读
  5. Kotlin函数式接口

    2024-01-11 17:42:04       57 阅读
  6. C# 的SequenceEqual

    2024-01-11 17:42:04       47 阅读
  7. 如何防止 DNS 攻击造成的损失

    2024-01-11 17:42:04       51 阅读
  8. PostgreSQL 归档和基于时间点恢复

    2024-01-11 17:42:04       49 阅读