「这是我参与11月更文挑战的第17天,活动详情查看:2021最后一次更文挑战」
描述
Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed:
- Find the leftmost occurrence of the substring part and remove it from s.
Return s after removing all occurrences of part.
A substring is a contiguous sequence of characters in a string.
Example 1:
1 | ini复制代码Input: s = "daabcbaabcbc", part = "abc" |
Example 2:
1 | ini复制代码Input: s = "axxxxyyyyb", part = "xy" |
Note:
1 | yaml复制代码1 <= s.length <= 1000 |
解析
根据题意,给出一个字符串 s 和一个字符串 part ,对 s 执行以下操作,直到删除所有出现的子字符串 part:
- 找到最左边出现的子字符串 part 并将其从 s 中删除
删除所有出现的 part 后返回 s。同时题目还好心给出了子字符串的含义:子字符串是字符串中连续的字符序列。
其实看完题目之后我们就知道了,这个题很简单,就是考察字符串中的对字符的查找和索引的基本操作,使用一个 while 循环,使用 python 的内置函数 find 如果在 s 中能找到最左边 part 出现的起始索引 i 就让,就让 s[:i] + s[i+len(part):] 替换旧的 s ,直到 while 循环无法从 s 中找到 part 为止,最后返回 s 即可。
解答
1 | python复制代码class Solution(object): |
运行结果
1 | erlang复制代码Runtime: 16 ms, faster than 94.40% of Python online submissions for Remove All Occurrences of a Substring. |
原题链接:leetcode.com/problems/re…
您的支持是我最大的动力
本文转载自: 掘金