|
| 1 | +# Replace Words |
| 2 | + |
| 3 | +In this problem, we are considering the words that are composed of a prefix and a postfix. For example, if we append a |
| 4 | +postfix “happy” to a prefix “un”, it forms the word “unhappy”. Similarly, “disagree” is formed from a prefix, “dis” |
| 5 | +followed by a postfix, “agree”. |
| 6 | + |
| 7 | +You’re given a dictionary, dictionary, consisting of prefixes, and a sentence, sentence, which has words separated by |
| 8 | +spaces only. Your task is to replace the postfix in sentence with their prefixes given in dictionary (if found) and |
| 9 | +return the modified sentence. |
| 10 | + |
| 11 | +A couple of points to keep in mind: |
| 12 | + |
| 13 | +- If a postfix in the sentence matches more than one prefix in the dictionary, replace it with the prefix that has the |
| 14 | + shortest length. For example, if we have the sentence “iphone is amazing”, and the dictionary {“i”, “ip”, “hone”}, |
| 15 | + then the word “iphone” has two prefixes in the dictionary “i” and “ip”, but we will replace it with the one that is |
| 16 | + shorter among the two, that is, “i”. |
| 17 | +- If there is no root word against any word in the sentence, leave it unchanged. |
| 18 | + |
| 19 | +## Constraints |
| 20 | + |
| 21 | +- 1 <= dictionary.length <= 1000 |
| 22 | +- 1 <= `dictionary[i].length` <= 100 |
| 23 | +- `dictionary[i]` consists of only lower-case letters. |
| 24 | +- 1 <= `sentence.length` <= 10^6 |
| 25 | +- `sentence` consists of only lower-case letters and spaces. |
| 26 | +- The number of words in `sentence` is in the range `[1, 1000]` |
| 27 | +- The length of each word in `sentence` is in the range `[1, 1000]` |
| 28 | +- Every two consecutive words in `sentence` will be separated by exactly one space. |
| 29 | +- `sentence` does not have leading or trailing spaces. |
| 30 | + |
| 31 | +## Examples |
| 32 | + |
| 33 | +Example 1: |
| 34 | + |
| 35 | +```text |
| 36 | +Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery" |
| 37 | +Output: "the cat was rat by the bat" |
| 38 | +``` |
| 39 | + |
| 40 | +Example 2: |
| 41 | + |
| 42 | +```text |
| 43 | +Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs" |
| 44 | +Output: "a a b c" |
| 45 | +``` |
| 46 | + |
| 47 | +Example 3: |
| 48 | + |
| 49 | +```text |
| 50 | +Input: dictionary = ["a", "aa", "aaa", "aaaa"], sentence = "a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa" |
| 51 | +Output: "a a a a a a a a bbb baba a" |
| 52 | +``` |
| 53 | + |
| 54 | +Example 4: |
| 55 | + |
| 56 | +```text |
| 57 | +Input: dictionary = ["catt","cat","bat","rat"], sentence = "the cattle was rattled by the battery" |
| 58 | +Output: "the cat was rat by the bat" |
| 59 | +``` |
| 60 | + |
| 61 | +Example 5: |
| 62 | + |
| 63 | +```text |
| 64 | +Input: dictionary = ["ac","ab"], sentence = "it is abnormal that this solution is accepted" |
| 65 | +Output: "it is ab that this solution is ac" |
| 66 | +``` |
| 67 | + |
| 68 | +## Topics |
| 69 | + |
| 70 | +- Array |
| 71 | +- Hash Table |
| 72 | +- String |
| 73 | +- Trie |
| 74 | + |
| 75 | +## Solution(s) |
| 76 | + |
| 77 | +1. [Naive Approach](#naive-approach) |
| 78 | +2. [Optimized approach using trie](#optimized-approach-using-trie) |
| 79 | + |
| 80 | +### Naive approach |
| 81 | + |
| 82 | +This problem requires us to observe the leading characters (prefixes) of words in a sentence that can be replaced with |
| 83 | +the shortest matching prefix from a dictionary. |
| 84 | + |
| 85 | +In the naive approach, we traverse the given sentence, and for each word, we compare it with each prefix in the given |
| 86 | +dictionary to find the shortest matching prefix against it, if any. To make sure that we get to the right prefix, we |
| 87 | +store the respective lengths of prefixes with them. Additionally, we keep track of the shortest prefix as well. If, at |
| 88 | +any point, a match is found, we compare its length with the length of the shortest match found up to that point. If it |
| 89 | +is shorter than the current shortest prefix, we update the value of the shortest prefix. |
| 90 | + |
| 91 | +In this approach, we iterate through each word in the sentence, which takes O(n) time, where n is the number of words in |
| 92 | +a sentence. For each word, we iterate over the prefixes in the dictionary, which takes O(d) time, where d is the number |
| 93 | +of prefixes in the dictionary. Therefore, the overall time complexity for this approach is O(n×d). |
| 94 | + |
| 95 | +### Optimized approach using trie |
| 96 | + |
| 97 | +Searching for the shortest prefixes for all the words in the given sentence requires repeated searches in the dictionary. |
| 98 | +While this would be an expensive proposition if we keep our dictionary words in data structures like arrays and linked |
| 99 | +lists, some more advanced data structures, such as tries, help reduce our search time complexity. In this approach, we |
| 100 | +first store all the dictionary prefixes in a trie and then find the shortest prefix against each word of the sentence so |
| 101 | +that we can perform the required replacement. |
| 102 | + |
| 103 | +> A trie is visualized as a tree of characters that allows storing strings as graphs. A trie stores a word in a top-down |
| 104 | +> manner where all words having common leading characters (prefixes) have a common parent. |
| 105 | +
|
| 106 | +To store the dictionary prefixes in the trie, we start from its root node and perform the following steps repeatedly |
| 107 | +until we’ve stored all the words: |
| 108 | + |
| 109 | +- We check if the current character of the dictionary prefix is listed among the immediate children of the current node. |
| 110 | + - If it doesn’t exist, we create a new trie node for this character as a child of the current node and move to this new |
| 111 | + trie node. |
| 112 | + - If it exists among the children, we simply move to that node. |
| 113 | +- If the current character is the last character of a given dictionary prefix, we use a flag to mark the trie node holding |
| 114 | + this last character. This flag represents the completion of a dictionary prefix on the trie. For example, if we have |
| 115 | + the prefix “dis” in the dictionary, then when we reach “s” and store it in the trie. We mark this node with the flag |
| 116 | + to represent that the word, “dis”, has been completed. |
| 117 | + |
| 118 | +After we are done creating the trie from the dictionary prefixes, we start to locate the matching prefix for each word |
| 119 | +of the sentence. Once found, we replace the word in the sentence with it. For example, if the input sentence is |
| 120 | +“fantastic” and the given dictionary is [“fanta”, “tic”, “fan”], then both “fanta” and “fan” are matching prefixes for |
| 121 | +“fantastic”, but we replace it with “fan” since it is the shortest of all the found matches. Since we are using a trie, |
| 122 | +our search automatically picks up the shortest matching prefix, and we don’t need to do it later. |
| 123 | + |
| 124 | +To traverse all the words in the sentence, we store them in a list. Then, for each word, wi, perform the following: |
| 125 | + |
| 126 | +- Start with the first character, `wi[0]`, and look at whether the same character exists in any of the children of the |
| 127 | + root node of the trie. |
| 128 | + - If it exists, it implies that the prefix for `wi` exists, and we can continue as follows: |
| 129 | + - For remaining characters, keep looking for the matching character in the trie one by one. |
| 130 | + - If at any point while searching and matching, we reach a node that is flagged for being the last character, we |
| 131 | + stop and return this prefix as the shortest matching prefix for `wi` |
| 132 | + - Otherwise, the prefix against `wi` doesn’t exist, so just move to the next word of the sentence. |
| 133 | + |
| 134 | + |
| 135 | +Once found, replace the original word `wi` in the list with its matched prefix. Keep doing this until we process the |
| 136 | +last word in the sentence. In the end, change the list of words back to the string representing the modified sentence |
| 137 | +and return it as the output. |
| 138 | + |
| 139 | +#### Summary |
| 140 | + |
| 141 | +Let’s review the optimized approach we have discussed above. |
| 142 | + |
| 143 | +- Create a trie and store all the dictionary prefixes in it. |
| 144 | +- Traverse the sentence and use the trie to find the shortest matching prefix for each word of the sentence. |
| 145 | +- Once found, replace the original word of the sentence with the matched prefix. We do this for all the words in the |
| 146 | + sentence. |
| 147 | +- If no matching prefix is found, leave the word as it is and move to the next word in the sentence. |
| 148 | +- Return the modified sentence as the output. |
| 149 | + |
| 150 | +#### Time Complexity |
| 151 | + |
| 152 | +It takes O(m) time to create a trie from the given dictionary prefixes, where m is the sum of the number of characters |
| 153 | +of all the prefixes in the dictionary. |
| 154 | + |
| 155 | +Then, it takes O(n) time to iterate over each word in the sentence and find its matching prefix in the trie, where n is |
| 156 | +the number of words in the sentence. |
| 157 | + |
| 158 | +Therefore, the overall time complexity for this approach is O(n+m). |
| 159 | + |
| 160 | +#### Space Complexity |
| 161 | + |
| 162 | +The space complexity is O(m), where m is the sum of the number of characters of all the prefixes in the dictionary. |
0 commit comments