Editor

Longest Common Prefix

#53 · Strings · Intermediate · 55 XP · about 8 min

Write common_prefix(words) that returns the longest string that every word in words starts with. When the words do not even share their first letter, or the list is empty, return an empty string.

Examples

common_prefix(["interview", "internet", "interval", "internal"])
# returns 'inter'
common_prefix(["dog", "dot", "door"])  # returns 'do'
common_prefix(["dog", "cat", "cow"])   # returns ''

Constraints

  • words has 0 to 1,000 items.
  • Each word has 0 to 200 lowercase letters.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.