📘
Longest Common Prefix
EasyArrays & Strings
Given an array of strings, find the longest common prefix among all strings. If none, return "".
Example:
Input:
flower,flow,flight
Output:
fl
Reasoning:
- The input strings are compared character by character to find the common prefix:
flower,flow, andflight. - The first character
fis common to all strings, so it is added to the prefix. - The second character
lis also common to all strings, so it is added to the prefix, resulting infl. - Since the third character differs among the strings (
oinflowerandflow,iinflight), the comparison stops, and the common prefixflis returned as the output.
Constraints:
- 1 <= len(strs) <= 200
- 0 <= len(strs[i]) <= 200
- strs[i] consists of lowercase English letters
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.