Leetcode 745: Prefix and suffix search solution
For each possible word, we ask the question: What possible queries can lead to this word?. For example consider the word: abd. Below are the possible (prefix, suffix) queries that can return abd 1 2 3 4 5 6 7 8 9 10 (prefix, suffix) a abd a bd a d ab abd ab bd ab d abd abd abd bd abd d Now, given word.length <= 7. Each word can at maximum generate 7*7=49 such pairs. Can we store all of them in a hashmap? Yes, because the total storage = 49 * number of words = O(n) Which is acceptable for this problem. ...