Skip to content
BytePatterns

Longest Shared Prefix

EasyStrings#column-scan#string-comparison~15m

Problem

Given a collection of words, find the longest opening run of characters that every word begins with. The run must start at the first character of each word, so a match that appears later in a word does not count. Return the empty text when the words share no opening character.

Examples

Input:  words = ["flower", "flow", "flight"]
Output: "fl"
Why:    the third word breaks the agreement at the third character
Input:  words = ["dog", "racecar"]
Output: ""
Why:    the words disagree immediately
Input:  words = []
Output: ""
Why:    edge case, there are no words to compare

Hints

0 / 3

Stuck on the idea rather than the code? String Basics covers it.