I have a list of phrases:

```
[
  "according to all known laws of aviation",
  "time flies when you're having fun..."
  ...
]

```

I would like to find all the phrases in this list that match with the end of an input string. For example:

```
getNext("did you know that time flies w")

```

should perform a search through the list to match any string that starts with what `did you know that time flies w` ends with. As an example, it should return a list that at least contains `time flies when you're having fun` since itends with `time flies w` which is the start of that phrase.

I can't think of any reasonable way to do this. 

The only thing I can think of is running through each character in the input and check if it matches the start of the phrase. If it does, increment a counter. If it is >1 (or some other number) at the end of the loop, at the phrase to another list, along with the number of matches. From there I could sort the list to give the closest match first, and the less close ones last.

Surely there must be a better way to do this?

For a bit of background, you can see [this Repl.it project](https://repl.it/@davwheat/AutocompleteText) and [this very similar Python question](https://stackoverflow.com/questions/32458737/check-if-string-ends-with-the-beginning-of-another-string).