Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Longest Common Prefix

XPChallenge Points: 10
levelLevel: Easy

You are given an array of strings words.
Your task is to determine the longest common prefix shared among all strings in the array.
If there is no shared prefix, return an empty string "".
A common prefix is the portion at the beginning of each word that appears exactly the same across all given strings.

Example 1:

Input: words = ["interview", "internet", "internal", "interval"]

Output: "inter"

Explanation:

All words share the prefix "inte" before they start to differ.

Example 2:

Input: words = ["apple", "application", "apex"]

Output: "ap"

Explanation:

All three words start with "ap", but the next letters differ (p, p, and e).

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result