Skip to content
Decoder

Longest Common Prefix

Easy

Problem

Given a list of strings strs, return the longest prefix shared by every string. If there is no common prefix, return the empty string "".

Example 1

Input
strs = ["flower", "flow", "flight"]
Output
"fl"

Example 2

Input
strs = ["dog", "racecar", "car"]
Output
""

Example 3

Input
strs = ["alone"]
Output
"alone"

Constraints

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] contains only lowercase English letters.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.