Longest Common Prefix
Easy · Strings · String · Trie
Problem
Return the longest prefix shared by every string in strs. If there is none, return "".
Examples
Input: strs = ["flower","flow","flight"] Output: "fl"
Input: strs = ["dog","racecar","car"] Output: ""
Nothing is shared.
Constraints
- 1 <= len(strs) <= 200
- 0 <= len(strs[i]) <= 200
- Lowercase English letters
Approaches
- Compare column by column — time O(total characters), space O(1)
Related problems
Start free on Cariara · Coding problems · System design studio