作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/

题目描述

We are given that the string "abc" is valid.

From any valid string V, we may split V into two pieces X and Y such that X + Y (X concatenated with Y) is equal to V. (X or Y may be empty.) Then, X + "abc" + Y is also valid.

If for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". Examples of invalid strings are: "abccba", "ab", "cababc", "bac".

Return true if and only if the given string S is valid.

Example 1:

Input: "aabcbc"
Output: true
Explanation:
We start with the valid string "abc".
Then we can insert another "abc" between "a" and "bc", resulting in "a" + "abc" + "bc" which is "aabcbc".

Example 2:

Input: "abcabcababcc"
Output: true
Explanation:
"abcabcabc" is valid after consecutive insertings of "abc".
Then we can insert "abc" before the last letter, resulting in "abcabcab" + "abc" + "c" which is "abcabcababcc".

Example 3:

Input: "abccba"
Output: false

Example 4:

Input: "cababc"
Output: false

Note:

  1. 1 <= S.length <= 20000
  2. S[i] is 'a', 'b', or 'c'

题目大意

初始只给了一个"abc"字符串,然后可以在该字符串的任意位置插入一个新的"abc"字符串,然后继续该操作……这些步骤得到的字符串均为有效字符串。如果经过上面的操作无论如何都不能得到的字符串就是无效字符串。

问一个字符串是不是有效字符串。

解题方法

循环

好久没有做过这么简单的题目了……

如果理解了题意应该明白,每次都去插入一个"abc"字符串,那么到最后一步一定会有"abc"。所以我们如果倒着来,每次把"abc"进行替换成"",那么一定能回到最初的状态,也就是空字符串。

这个方法唯一不太好的是时间复杂度,判断"abc"是否在S中,需要O(N)的时间复杂度,替换这步的操作应该也是O(N),所以总的时间复杂度是O(N^2)。题目给出的S的长度是20000,为什么没有超时呢?我觉得主要是替换的时候会把大量的"abc"同时替换掉了,这样每次操作就把字符串大幅变短了。

python代码如下:

class Solution(object):
def isValid(self, S):
"""
:type S: str
:rtype: bool
"""
while "abc" in S:
S = S.replace("abc", "")
return not S

日期

2019 年 3 月 3 日 —— 3月开始,春天到了

【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)的更多相关文章

  1. 【leetcode】1003. Check If Word Is Valid After Substitutions

    题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...

  2. 1003. Check If Word Is Valid After Substitutions

    We are given that the string "abc" is valid. From any valid string V, we may split V into ...

  3. 1003. Check If Word Is Valid After Substitutions Medium检查替换后的词是否有效

    网址:https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/ 参考:https://leetcode.com ...

  4. [Swift]LeetCode1003. 检查替换后的词是否有效 | Check If Word Is Valid After Substitutions

    We are given that the string "abc" is valid. From any valid string V, we may split V into ...

  5. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  6. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...

  7. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  8. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  9. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. 23-Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  2. 内存管理malloc 2

    malloc可以在函数指针内使用.#include <stdio.h> #include <stdlib.h> char * get_string() { //char s[] ...

  3. absurd, abundant

    absurd How: absolutely, completely, clearly, faintly, manifestly, obviously, patently, quite, rather ...

  4. 【leetcode】43. Multiply Strings(大数相乘)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  5. HttpClient连接池设置引发的一次雪崩

    事件背景 我在凤巢团队独立搭建和运维的一个高流量的推广实况系统,是通过HttpClient 调用大搜的实况服务.最近经常出现Address already in use (Bind failed)的问 ...

  6. 如何将List集合中相同属性的对象合并

    在实际的业务处理中,我们经常会碰到需要合并同一个集合内相同属性对象的情况,比如,同一个用户短时间内下的订单,我们需要将各个订单的金额合并成一个总金额.那么用lambda表达式和HashMap怎么分别处 ...

  7. JDBC(3):PreparedStatement对象介绍

    一,PreparedStatement介绍 PreperedStatement是Statement的子类,它的实例对象可以通过Connection.preparedStatement()方法获得,相对 ...

  8. 【编程思想】【设计模式】【行为模式Behavioral】状态模式State

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/state.py #!/usr/bin/env pytho ...

  9. 【JAVA】【Basic】概念

    1. 历史 1.1. Sun, Green Project, 90年代初,为机顶盒提供一个统一的语言层,oak-->Java, James Gosling, Sun World 1995:JAV ...

  10. 【JavaWeb】【学习】【过滤器】Filter 的简单应用

    实现效果 在编辑框中输入暗号:如果暗号正确,则跳转到正确页面:如果暗号错误,则跳转到错误界面. [入口界面] [错误界面] [成功界面] [项目结构] JSP文件 本练习有两个jsp页面 页面1:in ...