Longest Common Prefix
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 "". All given inputs are lowercase English letters.
[ "[\"flower\",\"flow\",\"flight\"]" ]
Explanation. The longest common prefix among "flower", "flow", and "flight" is "fl".
[ "[\"dog\",\"racecar\",\"car\"]" ]
Explanation. There is no common prefix among the input strings.
[ "[\"apple\"]" ]
Explanation. With only one string, the string itself is the longest common prefix.
[ "[\"apple\",\"apple\",\"apple\"]" ]
Explanation. All strings are identical, so the string itself is the longest common prefix.
[ "[\"\",\"b\"]" ]
Explanation. One of the strings is empty, so no common prefix can exist.
[ "[]" ]
Explanation. The input array is empty, so there's no common prefix to find.
[ "[\"ab\",\"a\"]" ]
Explanation. The common prefix is 'a'.
[ "[\"abcdefgh\",\"abcdef\",\"abcde\"]" ]
Explanation. The longest common prefix is determined by the shortest string's common part.
Follow-up: Can you optimize your solution for cases where the input array `strs` is extremely large (e.g., millions of strings), or when the strings themselves are very long?
- 1 <= strs.length <= 200 - 0 <= strs[i].length <= 200 - strs[i] consists of only lowercase English letters.
- Views
- 2