返回介绍

solution / 1000-1099 / 1003.Check If Word Is Valid After Substitutions / README_EN

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

1003. Check If Word Is Valid After Substitutions

中文文档

Description

Given a string s, determine if it is valid.

A string s is valid if, starting with an empty string t = "", you can transform t into s after performing the following operation any number of times:

  • Insert string "abc" into any position in t. More formally, t becomes tleft + "abc" + tright, where t == tleft + tright. Note that tleft and tright may be empty.

Return true _if _s_ is a valid string, otherwise, return_ false.

 

Example 1:

Input: s = "aabcbc"
Output: true
Explanation:
"" -> "abc" -> "aabcbc"
Thus, "aabcbc" is valid.

Example 2:

Input: s = "abcabcababcc"
Output: true
Explanation:
"" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
Thus, "abcabcababcc" is valid.

Example 3:

Input: s = "abccba"
Output: false
Explanation: It is impossible to get "abccba" using the operation.

 

Constraints:

  • 1 <= s.length <= 2 * 104
  • s consists of letters 'a', 'b', and 'c'

Solutions

Solution 1: Stack

If the string is valid, it's length must be the multiple of $3$.

We traverse the string and push every character into the stack $t$. If the size of stack $t$ is greater than or equal to $3$ and the top three elements of stack $t$ constitute the string "abc", we pop the top three elements. Then we continue to traverse the next character of the string $s$.

When the traversal is over, if the stack $t$ is empty, the string $s$ is valid, return true, otherwise return false.

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 isValid(self, s: str) -> bool:
    if len(s) % 3:
      return False
    t = []
    for c in s:
      t.append(c)
      if ''.join(t[-3:]) == 'abc':
        t[-3:] = []
    return not t
class Solution {
  public boolean isValid(String s) {
    if (s.length() % 3 > 0) {
      return false;
    }
    StringBuilder t = new StringBuilder();
    for (char c : s.toCharArray()) {
      t.append(c);
      if (t.length() >= 3 && "abc".equals(t.substring(t.length() - 3))) {
        t.delete(t.length() - 3, t.length());
      }
    }
    return t.isEmpty();
  }
}
class Solution {
public:
  bool isValid(string s) {
    if (s.size() % 3) {
      return false;
    }
    string t;
    for (char c : s) {
      t.push_back(c);
      if (t.size() >= 3 && t.substr(t.size() - 3, 3) == "abc") {
        t.erase(t.end() - 3, t.end());
      }
    }
    return t.empty();
  }
};
func isValid(s string) bool {
  if len(s)%3 > 0 {
    return false
  }
  t := []byte{}
  for i := range s {
    t = append(t, s[i])
    if len(t) >= 3 && string(t[len(t)-3:]) == "abc" {
      t = t[:len(t)-3]
    }
  }
  return len(t) == 0
}
function isValid(s: string): boolean {
  if (s.length % 3 !== 0) {
    return false;
  }
  const t: string[] = [];
  for (const c of s) {
    t.push(c);
    if (t.slice(-3).join('') === 'abc') {
      t.splice(-3);
    }
  }
  return t.length === 0;
}

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

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

发布评论

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