|
| 1 | +# Index Pairs of a String |
| 2 | + |
| 3 | +Given a string text and an array of strings words, return a list of all index pairs [i, j] such that the substring |
| 4 | +`text[i...j]` is present in words. |
| 5 | + |
| 6 | +Return the pairs [i, j] in a sorted order, first by the value of i, and if two pairs have the same i, by the value of j. |
| 7 | + |
| 8 | +## Constraints |
| 9 | + |
| 10 | +- 1 ≤ text.length ≤ 100 |
| 11 | +- 1 ≤ words.length ≤ 20 |
| 12 | +- 1 ≤ words[i].length ≤ 50 |
| 13 | +- text and words[i] consist of lowercase English letters. |
| 14 | +- All the strings of words are unique. |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +The algorithm uses a trie to find all pairs of start and end indexes for substrings in a given text that match words |
| 25 | +from a list. First, it builds the trie by inserting each word from the list. Then, it iterates over each starting index |
| 26 | +in the text to match substrings using the trie. For each character sequence, it checks if the current character exists |
| 27 | +in the trie and traverses accordingly. If a word’s end is found (marked by a flag), the start and end indexes of the |
| 28 | +matched substring are recorded in the result. This method optimizes substring searching using the trie structure to |
| 29 | +avoid redundant checks and efficiently match multiple words in the text. |
| 30 | + |
| 31 | +The algorithm to solve this problem is as follows: |
| 32 | + |
| 33 | +1. Insert each word from the list into the trie. Each character is added as a node, and isEndOfWord is set to mark the |
| 34 | + end of a word. |
| 35 | +2. Loop through each character in text (starting at index i). For each starting index, try to find substrings that match |
| 36 | + words in the trie by traversing them. |
| 37 | +3. For each character at position i, the algorithm begins traversing the trie from the root node. It then checks whether |
| 38 | + each subsequent character (from index i to j) is a child node in the trie. If the character is found, the traversal |
| 39 | + continues to the next character. A valid match has been found if the current node in the Trie marks the end of a word |
| 40 | + (i.e., isEndOfWord is True). In that case, the index pair [i, j] is recorded, where i is the start index, and j is the |
| 41 | + end index of the matched word. |
| 42 | +4. After checking all starting indexes, return the list of index pairs representing matched words’ start and end positions. |
| 43 | + |
| 44 | +### Time Complexity |
| 45 | + |
| 46 | +Inserting n words of average length m into the trie takes O(n∗m). For each index i in text, we perform a search that |
| 47 | +takes linear time in the length of the substring. This gives an overall time complexity of O(l∗k), where l is the length |
| 48 | +of the text, and k is the average length of a word. |
| 49 | + |
| 50 | +### Space Complexity |
| 51 | + |
| 52 | +The space the trie uses depends on the number of characters in the list words. If there are n words with an average length |
| 53 | +of m, the trie can take up to `O(n∗m)` space, assuming no overlapping prefixes among the words. In the worst case, if all |
| 54 | +words are unique and have no shared prefixes, each character is stored separately. |
| 55 | + |
| 56 | +The result list stores the index pairs [i, j]. In the worst case, every possible substring of text could be a word in |
| 57 | +the list words. This would result in `O(l^2)` pairs, where l is the length of the string text. So, the space complexity |
| 58 | +for the result list is `O(l^2)` in the worst case. |
| 59 | + |
| 60 | +Thus, the overall space complexity is `O(n∗m+l^2)`. |
0 commit comments