Find the shortest word window containing all query terms
Search results show a short snippet of each document with the query terms highlighted. The best snippet is the shortest run of consecutive words that contains every query term. Implement shortestSnippet(words, query), which finds it.
shortestSnippet(words, query) // => [start, end] | null
words is the document as an array of words. query is an array of terms.[start, end] of the shortest window words[start..end] that contains every query term.k times in query must appear at least k times in the window. Other words in the window do not matter.toLowerCase()). Otherwise they must be equal: punctuation is part of a word, so 'error:' does not match 'error'.null if no window contains every term, or if query is empty.const words = 'the quick brown fox jumps over the lazy dog'.split(' ');
// 0 1 2 3 4 5 6 7 8
shortestSnippet(words, ['dog', 'the']); // => [6, 8]
shortestSnippet(words, ['fox', 'the']); // => [0, 3] ([3, 6] is as short but starts later)
shortestSnippet(words, ['the', 'the']); // => [0, 6] (needs two "the")
shortestSnippet(words, ['LAZY', 'Dog']); // => [7, 8]
shortestSnippet(words, ['over']); // => [5, 5]
shortestSnippet(words, ['the', 'the', 'the']); // => null
shortestSnippet(words, ['cat']); // => null
shortestSnippet(words, []); // => null
const errands = 'buy milk then buy eggs then milk again'.split(' ');
shortestSnippet(errands, ['milk', 'eggs']); // => [4, 6]
shortestSnippet(errands, ['milk', 'buy', 'eggs']); // => [1, 4]