Match Dictionary Words to a Swipe-Keyboard Gesture Path
GoogleSoftware Engineer · OnsiteLast updated
A coding problem modeled on a swipe keyboard: given the ordered keys a finger passed over and a dictionary, return every word the gesture could spell. It tests precise handling of the start and end keys, doubled letters and duplicate words, and efficient matching of many dictionary words against one gesture path.
On a swipe keyboard, the user drags one finger across the letters instead of tapping each key, and the keyboard has to work out which word was meant. The gesture is recorded as the ordered list of keys the finger passed over, from the key where it touched down to the key where it lifted. Given that path and a dictionary, return every dictionary word the gesture could spell.
The interview version was ambiguous about exactly when a word counts as a match; this practice version fixes the rules below so that the answer is unique.
Function Signature
def swipe_matches(path: str, words: list[str]) -> list[str]:
Rules
pathis the sequence of keys the finger crossed, in order. A letter may appear in it more than once.- Let
n = len(path). A wordwwithm = len(w)letters matchespathwhen every letterw[j]can be assigned a positioni[j]inpathsuch that: path[i[j]] == w[j]for everyj;i[0] == 0: the word starts on the key where the gesture started;i[m - 1] == n - 1: the word ends on the key where the gesture ended;- positions never move backwards:
i[0] <= i[1] <= ... <= i[m - 1]. - Because two consecutive letters may share a position, a doubled letter such as the
llinhellocan be spelled by passing over a singlelkey once. - It follows that a one-letter word matches only when
pathitself is that single letter. - Return each distinct matching word once, sorted in ascending lexicographic order (plain character comparison). Return an empty list if nothing matches.
Constraints
1 <= len(path) <= 20001 <= len(words) <= 100001 <= len(w) <= 30for everywinwords- The total number of letters across all of
wordsis at most200000. pathand every word contain only lowercase English lettersatoz.wordsmay contain duplicates and is in no particular order.
Examples
Example 1
path = "hgreasdfghjklo"
words = ["hello", "halo", "hero", "help", "hallo", "halo", "hole"]
output = ["hallo", "halo", "hello"]
hello:hat 0,eat 3, bothls at 12,oat 13, the last position.haloandhallo:hat 0,aat 4,lat 12,oat 13.haloappears twice inwordsbut once in the output.herofails because the onlyr(position 2) comes before the onlye(position 3).helpandholefail because the gesture ends ono, not on their last letter.
Example 2
path = "abcd"
words = ["add", "abd", "acbd", "a", "abbd", "bad"]
output = ["abbd", "abd", "add"]
acbdfails: oncecis placed at position 2, there is nobat or after it.afails: a one-letter word cannot span a four-key gesture.badfails: the gesture starts ona.
Example 3
path = "a"
words = ["a", "aa", "ab"]
output = ["a", "aa"]
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.