Submissions disabled: This deployment is running in read-only mode for safety. Code execution is not available here.
← Back to all problems
Easy

Longest Common Prefix

Updated Jan 31, 2026

Problem

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Constraints

- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] consists of only lowercase English letters

Examples

Example 1

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

Example 2

Input: strs = ["dog", "racecar", "car"]
Output: ""
There is no common prefix among the input strings.

Function Signature

def longestCommonPrefix(self, strs: list[str]) -> str

How to Submit

Implement a Solution class with a longestCommonPrefix method.

Your method will be called with the input parameters and should return the answer.