# 833. 字符串中的查找与替换 - LeetCode Python/Java/C++/JS/C#/Go/Ruby 题解 > 🚀 **打造你的开发者个人IP** > > 掌握算法是成功的基石,而全方位展示你的才华则是获得垂青的关键。 > > 我的另一个项目 [**leader.me**](https://www.leader.me) —— 专为程序员打造的“全能型”个人品牌展示平台。 > > **三位一体(All-In-One)的职场利器:** > - 📄 **简历 + 作品集 + 博客:** 将你的 GitHub 项目、技术心得与职场经历完美融合。 > - 🌐 **永久免费自定义域名:** 支持绑定你自己的独立域名,且该功能永久免费。 > - ✨ **顶级行业子域名:** 提供 `name.leader.me`,极具职业含金量的专属域名。 > > [**立即前往 leader.me 打造你的个人品牌 →**](https://www.leader.me) --- 访问原文链接:[833. 字符串中的查找与替换 - LeetCode Python/Java/C++/JS/C#/Go/Ruby 题解](https://leetcode.blog/zh/leetcode/833-find-and-replace-in-string),体验更佳! 力扣链接:[833. 字符串中的查找与替换](https://leetcode.cn/problems/find-and-replace-in-string), 难度等级:**中等**。 ## LeetCode “833. 字符串中的查找与替换”问题描述 你会得到一个字符串 `s` (索引从 0 开始),你必须对它执行 `k` 个替换操作。替换操作以三个长度均为 `k` 的并行数组给出:`indices`, `sources`, `targets`。 要完成第 `i` 个替换操作: 1. 检查 **子字符串** `sources[i]` 是否出现在 **原字符串** `s` 的索引 `indices[i]` 处。 2. 如果没有出现, **什么也不做** 。 3. 如果出现,则用 `targets[i]` **替换** 该子字符串。 例如,如果 `s = "abcd"` , `indices[i] = 0` , `sources[i] = "ab"`, `targets[i] = "eee"` ,那么替换的结果将是 `"eeecd"` 。 所有替换操作必须 **同时** 发生,这意味着替换操作不应该影响彼此的索引。测试用例保证元素间**不会重叠** 。 - 例如,一个 `s = "abc"` , `indices = [0,1]` ,`sources = ["ab","bc"]` 的测试用例将不会生成,因为 `"ab"` 和 `"bc"` 替换重叠。 在对 `s` 执行所有替换操作后返回 **结果字符串** 。 **子字符串** 是字符串中连续的字符序列。 ### [示例 1] ![](../../images/examples/833_1.png) **输入**: `s = "abcd", indices = [0,2], sources = ["a","cd"], targets = ["eee","ffff"]` **输出**: `"eeebffff"` **解释**:

"a" 从 s 中的索引 0 开始,所以它被替换为 "eee"。
"cd" 从 s 中的索引 2 开始,所以它被替换为 "ffff"。

### [示例 2] ![](../../images/examples/833_2.png) **输入**: `s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"]` **输出**: `"eeecd"` **解释**:

"ab" 从 s 中的索引 0 开始,所以它被替换为 "eee"。
"ec" 没有从原始的 S 中的索引 2 开始,所以它没有被替换。

### [约束] - `1 <= s.length <= 1000` - `k == indices.length == sources.length == targets.length` - `1 <= k <= 100` - `0 <= indexes[i] < s.length` - `1 <= sources[i].length, targets[i].length <= 50` - `s` consists of only lowercase English letters. - `sources[i]` and `targets[i]` consist of only lowercase English letters. ## 思路 这道题目看起来简单,做起来还挺花时间的。 - 问题一:对于所求的目标字符串`result`,可以基于原字符串的克隆,也可以从空字符串构建,哪个更好呢?
点击查看答案

基于原字符串的克隆比较好。因为你省去了不少子字符串的赋值操作。

- 问题二:在用`targets[i]`替换`result`的子字符串后,`result`的长度可能会变化,这导致后面的替换变得困难。如何解决?
点击查看答案

用技术手段让`result`的长度,在经历字符串替换后,始终保持不变。

## 复杂度 > N = s.length - 时间复杂度: `O(N)`. - 空间复杂度: `O(N)`. ## Python ```python class Solution: def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str: # Each item of this array is a string, not a char! We won't change the size of this array. result = list(s) for i in range(len(indices)): index = indices[i] if s[index:index + len(sources[i])] == sources[i]: for j in range(index, index + len(sources[i])): if j == index: result[j] = targets[i] else: result[j] = '' return ''.join(result) ``` ## Ruby ```ruby # @param {String} s # @param {Integer[]} indices # @param {String[]} sources # @param {String[]} targets # @return {String} def find_replace_string(s, indices, sources, targets) # Each item of this array is a string, not a char! We won't change the size of this array. result = s.clone.chars indices.each_with_index do |index, i| if s[index...index + sources[i].size] == sources[i] result[index] = targets[i] # The first one keep the whole targets[i] (1...sources[i].size).each do |j| result[index + j] = "" end end end result.join("") end ``` ## Other languages ```java // Welcome to create a PR to complete the code of this language, thanks! ``` > 🚀 **打造你的开发者个人IP** > > 掌握算法是成功的基石,而全方位展示你的才华则是获得垂青的关键。 > > 我的另一个项目 [**leader.me**](https://www.leader.me) —— 专为程序员打造的“全能型”个人品牌展示平台。 > > **三位一体(All-In-One)的职场利器:** > - 📄 **简历 + 作品集 + 博客:** 将你的 GitHub 项目、技术心得与职场经历完美融合。 > - 🌐 **永久免费自定义域名:** 支持绑定你自己的独立域名,且该功能永久免费。 > - ✨ **顶级行业子域名:** 提供 `name.leader.me`,极具职业含金量的专属域名。 > > [**立即前往 leader.me 打造你的个人品牌 →**](https://www.leader.me) --- 访问原文链接:[833. 字符串中的查找与替换 - LeetCode Python/Java/C++/JS/C#/Go/Ruby 题解](https://leetcode.blog/zh/leetcode/833-find-and-replace-in-string),体验更佳! GitHub 仓库: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).