Given a string text and an array of strings words, return an array of all the index pairs [i, j] so that the substring text[i...j] is in the array words. You should sort the [i, j] pairs in lexicographical order (i.e., sort the first index in ascending order, and if two pairs have the same first index, sort them by the second index in ascending order).
Example 1:
Input: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"] Output: [[3,7],[9,13],[10,17]]
Example 2:
Input: text = "ababa", words = ["aba","ab"]
Output: [[0,1],[0,2],[2,3],[2,4]]
Explanation:
Notice that the output is sorted lexicographically.
Constraints:
1 <= text.length <= 1001 <= words.length <= 1001 <= words[i].length <= 30text and words[i] consist of lowercase English letters.words are unique.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:
The brute force method to find pairs of starting and ending positions for specific words within a larger piece of text involves looking at every possible place a word could start and end. We painstakingly check each potential word against our list of target words. If a match is found, we record its starting and ending position.
Here's how the algorithm would work step-by-step:
def index_pairs_of_a_string_brute_force(text, words):
index_pairs = []
text_length = len(text)
for starting_position in range(text_length):
# Iterate through the target words to check for matches.
for target_word in words:
word_length = len(target_word)
# Check if the target word fits within the remaining text
if starting_position + word_length <= text_length:
substring = text[starting_position:starting_position + word_length]
if substring == target_word:
ending_position = starting_position + word_length - 1
index_pairs.append([starting_position, ending_position])
return index_pairsThe most efficient way to solve this problem is to search for each target word within the large string using a technique that avoids repeatedly checking the same locations. By pre-processing the list of target words, we can quickly locate all occurrences within the main string.
Here's how the algorithm would work step-by-step:
def find_index_pairs(main_string, target_words):
index_pairs = []
target_word_set = set(target_words)
# Converting to set enables faster lookup
for i in range(len(main_string)):
for target_word in target_word_set:
if main_string[i:].startswith(target_word):
# Record the starting and ending indices
index_pairs.append([i, i + len(target_word) - 1])
# Sort index pairs to meet output requirements
index_pairs.sort()
return index_pairs| Case | How to Handle |
|---|---|
| Empty text string | Return an empty list of index pairs as there are no substrings. |
| Empty words array | Return an empty list of index pairs as there are no words to find. |
| Text string is null or undefined | Throw an IllegalArgumentException or return an empty list based on requirements. |
| Words array is null or undefined | Throw an IllegalArgumentException or return an empty list based on requirements. |
| Words array contains an empty string | Skip the empty string and proceed with the rest of the words, or throw an exception depending on specification. |
| Words array contains duplicate words | The algorithm should correctly identify all occurrences of each duplicated word in the text. |
| Word is a substring of another word in the words array | The algorithm should identify all occurrences of both substrings without interference. |
| Very long text string and very large words array; potential for performance issues | Consider optimizing the search algorithm (e.g., using Aho-Corasick or other efficient string matching algorithms) if performance degrades significantly. |