Asked at

Longest Common Prefix

Easy
Verified
TrieString~15 min

Given an array of strings strs, return the longest common prefix shared by all of them.

If there is none, return "". Shrink a candidate prefix until every string starts with it.

The input arrives as a single array strs.

Examples

in["flower", "flow", "flight"]
out"fl"

All three share the prefix fl.

in["dog", "racecar", "car"]
out""

No common prefix exists.

Constraints

  • 1 ≤ strs.length ≤ 200
  • 0 ≤ strs[i].length ≤ 200
  • strs[i] consists of lowercase English letters.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.