833. Find And Replace in String Medium
1/**
2 * [833] Find And Replace in String
3 *
4 * You are given a 0-indexed string s that you must perform k replacement operations on. The replacement operations are given as three 0-indexed parallel arrays, indices, sources, and targets, all of length k.
5 * To complete the i^th replacement operation:
6 * <ol>
7 * Check if the substring sources[i] occurs at index indices[i] in the original string s.
8 * If it does not occur, do nothing.
9 * Otherwise if it does occur, replace that substring with targets[i].
10 * </ol>
11 * For example, if s = "<u>ab</u>cd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement will be "<u>eee</u>cd".
12 * All replacement operations must occur simultaneously, meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will not overlap.
13 *
14 * For example, a testcase with s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the "ab" and "bc" replacements overlap.
15 *
16 * Return the resulting string after performing all replacement operations on s.
17 * A substring is a contiguous sequence of characters in a string.
18 *
19 * Example 1:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/12/833-ex1.png" style="width: 411px; height: 251px;" />
21 * Input: s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"]
22 * Output: "eeebffff"
23 * Explanation:
24 * "a" occurs at index 0 in s, so we replace it with "eee".
25 * "cd" occurs at index 2 in s, so we replace it with "ffff".
26 *
27 * Example 2:
28 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/12/833-ex2-1.png" style="width: 411px; height: 251px;" />
29 * Input: s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"]
30 * Output: "eeecd"
31 * Explanation:
32 * "ab" occurs at index 0 in s, so we replace it with "eee".
33 * "ec" does not occur at index 2 in s, so we do nothing.
34 *
35 *
36 * Constraints:
37 *
38 * 1 <= s.length <= 1000
39 * k == indices.length == sources.length == targets.length
40 * 1 <= k <= 100
41 * 0 <= indexes[i] < s.length
42 * 1 <= sources[i].length, targets[i].length <= 50
43 * s consists of only lowercase English letters.
44 * sources[i] and targets[i] consist of only lowercase English letters.
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/find-and-replace-in-string/
50// discuss: https://leetcode.com/problems/find-and-replace-in-string/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55 pub fn find_replace_string(s: String, indices: Vec<i32>, sources: Vec<String>, targets: Vec<String>) -> String {
56 String::new()
57 }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_833() {
68 }
69}
70
Back
© 2025 bowen.ge All Rights Reserved.