Leetcode 833: Find And Replace in String

grid47
grid47
Exploring patterns and algorithms
Aug 15, 2024 6 min read

You are given a string s and k replacement operations. Each replacement operation consists of an index, a source substring, and a target substring. For each operation, check if the source substring occurs at the given index in s. If it does, replace the substring with the target. All operations are performed simultaneously, meaning they do not affect each other’s indexing. The task is to return the modified string after all operations are applied.
Problem
Approach
Steps
Complexity
Input: The input consists of a string s and three arrays: indices, sources, and targets. Each element in indices represents the starting position of a source substring in s, and each corresponding element in sources is the substring to replace. The corresponding element in targets is the substring to replace sources with.
Example: Input: s = 'hello', indices = [0, 3], sources = ['he', 'lo'], targets = ['hi', 'ya']
Constraints:
• 1 <= s.length <= 1000
• k == indices.length == sources.length == targets.length
• 1 <= k <= 100
• 0 <= indices[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
Output: The output is the modified string after performing all replacement operations on s simultaneously.
Example: Output: 'hiya'
Constraints:
• The result should be the string after all replacements have been applied, without affecting the indexing of subsequent replacements.
Goal: The goal is to apply all k replacements to the string s and return the final modified string. Each replacement should be checked, and if the source substring matches at the specified index, it should be replaced by the target substring.
Steps:
• Step 1: Create a list of index-source-target triples to store the replacement operations.
• Step 2: Sort the replacement operations in descending order of their indices to ensure replacements do not interfere with each other.
• Step 3: For each operation, check if the source substring matches the substring at the given index in s. If it does, replace it with the target.
• Step 4: Return the final string after all replacements have been applied.
Goal: Ensure that the replacement operations do not overlap, and that each operation is applied only if the source substring matches at the specified index in the original string.
Steps:
• The replacement operations are guaranteed to not overlap.
Assumptions:
• The input string s and the replacement arrays are valid according to the constraints.
Input: Input: s = 'hello', indices = [0, 3], sources = ['he', 'lo'], targets = ['hi', 'ya']
Explanation: In this example, 'he' at index 0 is replaced by 'hi', and 'lo' at index 3 is replaced by 'ya', resulting in the string 'hiya'.

Input: Input: s = 'abcd', indices = [0, 2], sources = ['a', 'cd'], targets = ['eee', 'ffff']
Explanation: Here, 'a' at index 0 is replaced by 'eee', and 'cd' at index 2 is replaced by 'ffff', resulting in 'eeebffff'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus