返回介绍

solution / 2600-2699 / 2696.Minimum String Length After Removing Substrings / README_EN

发布于 2024-06-17 01:03:01 字数 5124 浏览 0 评论 0 收藏 0

2696. Minimum String Length After Removing Substrings

中文文档

Description

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.

Solutions

Solution 1: Stack

We traverse the string $s$. For the current character $c$ we are traversing, if the stack is not empty and the top element of the stack $top$ can form $AB$ or $CD$ with $c$, then we pop the top element of the stack, otherwise we push $c$ into the stack.

The number of remaining elements in the stack is the length of the final string.

In implementation, we can pre-place an empty character in the stack, so there is no need to judge whether the stack is empty when traversing the string. Finally, we can return the size of the stack minus one.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.

class Solution:
  def minLength(self, s: str) -> int:
    stk = [""]
    for c in s:
      if (c == "B" and stk[-1] == "A") or (c == "D" and stk[-1] == "C"):
        stk.pop()
      else:
        stk.append(c)
    return len(stk) - 1
class Solution {
  public int minLength(String s) {
    Deque<Character> stk = new ArrayDeque<>();
    stk.push(' ');
    for (char c : s.toCharArray()) {
      if ((c == 'B' && stk.peek() == 'A') || (c == 'D' && stk.peek() == 'C')) {
        stk.pop();
      } else {
        stk.push(c);
      }
    }
    return stk.size() - 1;
  }
}
class Solution {
public:
  int minLength(string s) {
    string stk = " ";
    for (char& c : s) {
      if ((c == 'B' && stk.back() == 'A') || (c == 'D' && stk.back() == 'C')) {
        stk.pop_back();
      } else {
        stk.push_back(c);
      }
    }
    return stk.size() - 1;
  }
};
func minLength(s string) int {
  stk := []byte{' '}
  for _, c := range s {
    if (c == 'B' && stk[len(stk)-1] == 'A') || (c == 'D' && stk[len(stk)-1] == 'C') {
      stk = stk[:len(stk)-1]
    } else {
      stk = append(stk, byte(c))
    }
  }
  return len(stk) - 1
}
function minLength(s: string): number {
  const stk: string[] = [''];
  for (const c of s) {
    if (c === 'B' && stk.at(-1)! === 'A') {
      stk.pop();
    } else if (c === 'D' && stk.at(-1)! === 'C') {
      stk.pop();
    } else {
      stk.push(c);
    }
  }
  return stk.length - 1;
}
impl Solution {
  pub fn min_length(s: String) -> i32 {
    let mut ans: Vec<u8> = Vec::new();

    for c in s.bytes() {
      if let Some(last) = ans.last() {
        if *last == b'A' && c == b'B' {
          ans.pop();
        } else if *last == b'C' && c == b'D' {
          ans.pop();
        } else {
          ans.push(c);
        }
      } else {
        ans.push(c);
      }
    }

    ans.len() as i32
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文