LeetCode 题解工作台

检查替换后的词是否有效

给你一个字符串 s ,请你判断它是否 有效 。 字符串 s 有效 需要满足:假设开始有一个空字符串 t = "" ,你可以执行 任意次 下述操作将 t 转换为 s : 将字符串 "abc" 插入到 t 中的任意位置。形式上, t 变为 t left + "abc" + t right ,其中 t =…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 栈·状态

bolt

答案摘要

我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 ,所以每次插入操作之后,字符串的长度都会增加 。如果字符串 有效,那么它的长度一定是 的倍数。因此,我们先对字符串 的长度进行判断,如果不是 的倍数,那么 一定无效,可以直接返回 。 接下来我们遍历字符串 的每个字符 ,我们先将字符 压入栈 中。如果此时栈 的长度大于等于 ,并且栈顶的三个元素组成了字符串 ,…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 栈·状态 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串 s ,请你判断它是否 有效

字符串 s 有效 需要满足:假设开始有一个空字符串 t = "" ,你可以执行 任意次 下述操作将 t 转换为 s

  • 将字符串 "abc" 插入到 t 中的任意位置。形式上,t 变为 tleft + "abc" + tright,其中 t == tleft + tright 。注意,tlefttright 可能为

如果字符串 s 有效,则返回 true;否则,返回 false

 

示例 1:

输入:s = "aabcbc"
输出:true
解释:
"" -> "abc" -> "aabcbc"
因此,"aabcbc" 有效。

示例 2:

输入:s = "abcabcababcc"
输出:true
解释:
"" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
因此,"abcabcababcc" 有效。

示例 3:

输入:s = "abccba"
输出:false
解释:执行操作无法得到 "abccba" 。

 

提示:

  • 1 <= s.length <= 2 * 104
  • s 由字母 'a''b''c' 组成
lightbulb

解题思路

方法一:栈

我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 "abc"\textit{"abc"},所以每次插入操作之后,字符串的长度都会增加 33。如果字符串 ss 有效,那么它的长度一定是 33 的倍数。因此,我们先对字符串 ss 的长度进行判断,如果不是 33 的倍数,那么 ss 一定无效,可以直接返回 false\textit{false}

接下来我们遍历字符串 ss 的每个字符 cc,我们先将字符 cc 压入栈 tt 中。如果此时栈 tt 的长度大于等于 33,并且栈顶的三个元素组成了字符串 "abc"\textit{"abc"},那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 ss 的下一个字符。

遍历结束之后,如果栈 tt 为空,那么说明字符串 ss 有效,返回 true\textit{true};否则,返回 false\textit{false}

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 是字符串 ss 的长度。

1
2
3
4
5
6
7
8
9
10
11
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
speed

复杂度分析

指标
时间complexity is O(n) since each character is processed once. Space complexity is O(n) due to the stack storing characters for partial 'abc' sequences.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Focuses on stack-based state validation rather than brute force generation.

  • question_mark

    Checks understanding of sequence pattern enforcement with linear space.

  • question_mark

    Wants confirmation of handling edge cases where sequences overlap or are incomplete.

warning

常见陷阱

外企场景
  • error

    Failing to remove completed 'abc' sequences properly from the stack.

  • error

    Assuming any permutation of 'abc' is valid instead of the exact order.

  • error

    Not handling overlapping sequences like 'aabcbc' correctly.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Check if string is valid with a different fixed substring instead of 'abc'.

  • arrow_right_alt

    Allow nested or overlapping insertion rules and validate resulting string.

  • arrow_right_alt

    Count how many valid 'abc' sequences exist instead of boolean validation.

help

常见问题

外企场景

检查替换后的词是否有效题解:栈·状态 | LeetCode #1003 中等