|Longest Common Prefix
All Challenges

Longest Common Prefix

HardStrings

Given a list of strings words, return the longest common prefix shared by all words.


If there is no common prefix, return an empty string "".


This powers autocomplete — when you type in a search box, the app finds the longest prefix matching all suggestions and pre-fills it.

Examples

Input: longest_common_prefix(["flower", "flow", "flight"])
Output: "fl"
"fl" is common to all
Input: longest_common_prefix(["dog", "racecar", "car"])
Output: ""
No common prefix
Input: longest_common_prefix(["interview", "intercom"])
Output: "inter"
"inter" shared by both
Input: longest_common_prefix(["alone"])
Output: "alone"
Single word — itself is the prefix

Constraints

  • 1 <= len(words) <= 200
  • 1 <= len(words[i]) <= 200
  • words[i] contains only lowercase letters
pythoneditor
Loading Python runtime…