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.
To complete the ith replacement operation:
sources[i] occurs at index indices[i] in the original string s.targets[i].For example, if s = "abcd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement will be "eeecd".
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.
s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the "ab" and "bc" replacements overlap.Return the resulting string after performing all replacement operations on s.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"] Output: "eeebffff" Explanation: "a" occurs at index 0 in s, so we replace it with "eee". "cd" occurs at index 2 in s, so we replace it with "ffff".
Example 2:
Input: s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"] Output: "eeecd" Explanation: "ab" occurs at index 0 in s, so we replace it with "eee". "ec" does not occur at index 2 in s, so we do nothing.
Constraints:
1 <= s.length <= 1000k == indices.length == sources.length == targets.length1 <= k <= 1000 <= indexes[i] < s.length1 <= sources[i].length, targets[i].length <= 50s consists of only lowercase English letters.sources[i] and targets[i] consist of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given a main string and instructions to find and replace parts of it. The brute force method looks at every possible location in the main string where a replacement might be needed and performs the replacement if it matches the instruction. It methodically checks each position and applies or skips replacements based on direct comparison.
Here's how the algorithm would work step-by-step:
def find_and_replace_in_string_brute_force(main_string, indices, sources, targets):
modified_string = list(main_string)
for index_in_main_string in range(len(main_string)):
for i in range(len(indices)):
if indices[i] == index_in_main_string:
source_string = sources[i]
target_string = targets[i]
# Check if the source string matches the main string at current index
if main_string[index_in_main_string:index_in_main_string + len(source_string)] == source_string:
# Perform the replacement if there is a match
for replace_index in range(len(source_string)):
modified_string[index_in_main_string + replace_index] = ''
modified_string[index_in_main_string] = target_string
# Update main_string to reflect the change to prevent overlapping replaces
main_string = "".join(modified_string)
modified_string = list(main_string)
return "".join(modified_string)We will process the original string by figuring out which replacements need to happen first. We sort the replacements by their starting position to easily apply them in the correct order. This avoids messing up future replacement positions as we change the string.
Here's how the algorithm would work step-by-step:
def find_and_replace_in_string(string, indices, sources, targets):
replacements = []
for i in range(len(indices)):
replacements.append((indices[i], sources[i], targets[i]))
# Sort replacements by starting index
replacements.sort()
modified_string = list(string)
for index, source, target in replacements:
# Check if the source string matches at the given index
if string[index:index + len(source)] == source:
# This replacement is valid, proceed to apply.
modified_string[index:index + len(source)] = list(target)
return "".join(modified_string)| Case | How to Handle |
|---|---|
| Empty string | Return the empty string immediately, as there's nothing to modify. |
| Null input strings or arrays | Throw an IllegalArgumentException or return null/empty string after checking for null inputs. |
| Empty sources or targets array | Return the original string if either sources or targets array are empty. |
| Sources and targets arrays have different lengths | Throw an IllegalArgumentException indicating mismatched array lengths. |
| Overlapping source ranges | Sort the replacements by starting index and process them in order, skipping overlapping ranges or choosing the earliest one. |
| Source string is not found in the original string | Skip the replacement for that particular source string. |
| Very large string and numerous replacements | Use StringBuilder for efficient string manipulation to avoid repeated string concatenation. |
| Indexes array out of bounds | Ensure all indices in the 'indices' array are valid and within the bounds of the original string. |