Find All Word Occurrences in Text
Updated
Problem statement
You are given a block of text text and a list of words words. Find every place where a word from words appears in text.
Report each match as a pair [i, j], where text[i..j] (from index i to index j, both included) is exactly equal to one of the words. Indices start at 0.
Every match counts, even when two matches overlap or when a word appears inside a longer word. Because words contain no spaces, a match never crosses a space.
Return all pairs sorted by i, and pairs with the same i sorted by j. If no word appears, return an empty list.
Function
indexPairs(text: String, words: String[]) → int[][]Examples
Example 1
text = "the cat sat on the mat"words = ["the","at"]return = [[0,2],[5,6],[9,10],[15,17],[20,21]]The word "the" appears at indices 0 to 2 and 15 to 17. The word "at" appears inside "cat" (5 to 6), "sat" (9 to 10) and "mat" (20 to 21). Sorted by start index, the answer is [[0,2],[5,6],[9,10],[15,17],[20,21]].
Example 2
text = "ababa"words = ["aba","ab"]return = [[0,1],[0,2],[2,3],[2,4]]"ab" appears at 0 to 1 and 2 to 3. "aba" appears at 0 to 2 and again at 2 to 4, so the two matches share index 2. Pairs with the same start are ordered by end, so [0,1] comes before [0,2]. The answer is [[0,1],[0,2],[2,3],[2,4]].
Example 3
text = "hello world"words = ["xyz"]return = []"xyz" does not appear anywhere in the text, so the answer is an empty list.
Constraints
1 <= text.length <= 1000textcontains only lowercase English letters and spaces.1 <= words.length <= 201 <= words[i].length <= 50words[i]contains only lowercase English letters.- All strings in
wordsare distinct.
More Sentry interview questions
Similar questions at other companies
- Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Parse and Sort Integer DataLegora · PHONE SCREEN
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Remove Later Anagrams From a ListBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Employees With Shared InterestsPalantir · PHONE SCREENPalantir interview questions
- Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions