Strings · Coding problem

Longest Common Prefix

A free coding problem from the String Prefix Comparison skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

Find the longest string that every word in strs starts with. If the words don't all share even a first letter, return an empty string.

Example

Input:  strs = ["interview", "internet", "interval"]
Output: inter

Constraints

  • 1 <= strs.length <= 200
  • 1 <= strs[i].length <= 200 (the strings are read as space-separated words, so none is empty)
  • strs[i] consists of lowercase English letters

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in String Prefix Comparison

Solve it Longest Common Prefix free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime