You are given an array of strings strs. Return the longest common prefix of all the strings.
If there is no longest common prefix, return an empty string "".
Example 1:
Input: strs = ["bat","bag","bank","band"]
Output: "ba"Example 2:
Input: strs = ["dance","dag","danger","damage"]
Output: "da"Example 3:
Input: strs = ["neet","feet"]
Output: ""Constraints:
1 <= strs.length <= 2000 <= strs[i].length <= 200strs[i] is made up of lowercase English letters if it is non-empty.Start with the first string as the initial prefix candidate. Then compare it with each subsequent string, shrinking the prefix to match only the common portion. After processing all strings, what remains is the longest common prefix. The prefix can only shrink or stay the same as we go through more strings.
prefix as the first string in the array.j where characters stop matching (or we run out of characters in either string).prefix to include only characters from index 0 to j-1.Where is the length of the shortest string and is the number of strings.
Instead of comparing entire strings horizontally, we can compare characters column by column across all strings. Check if all strings have the same character at position 0, then position 1, and so on. The moment we find a mismatch or reach the end of any string, we've found where the common prefix ends.
0.i, check the character in the first string.i in every other string.i-1.Where is the length of the shortest string and is the number of strings.
When strings are sorted lexicographically, the first and last strings in the sorted order are the most different from each other. If these two extremes share a common prefix, then all strings in between must also share that same prefix. So we only need to compare the first and last strings after sorting.
Where is the length of the longest string and is the number of strings.
A Trie naturally represents all prefixes. We insert the shortest string into the trie, then query each other string against it. For each string, we walk down the trie as far as characters match, tracking how deep we get. The minimum depth reached across all strings is the length of the longest common prefix.
prefixLen to the length of the shortest string.prefixLen to be the minimum of its current value and how far we matched.prefixLen characters from any string as the result.class TrieNode:
def __init__(self):
self.children = {}
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word: str) -> None:
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
def lcp(self, word: str, prefixLen: int) -> int:
node = self.root
for i in range(min(len(word), prefixLen)):
if word[i] not in node.children:
return i
node = node.children[word[i]]
return min(len(word), prefixLen)
class Solution:
def longestCommonPrefix(self, strs: list[str]) -> str:
if len(strs) == 1:
return strs[0]
mini = 0
for i in range(1, len(strs)):
if len(strs[mini]) > len(strs[i]):
mini = i
trie = Trie()
trie.insert(strs[mini])
prefixLen = len(strs[mini])
for i in range(len(strs)):
prefixLen = trie.lcp(strs[i], prefixLen)
return strs[0][:prefixLen]Where is the length of the shortest string and is the number of strings.
If any string in the input array is empty, the longest common prefix must be an empty string. Failing to check for this case before accessing characters can lead to index out of bounds errors.
When comparing characters at a given index, you must ensure the index is valid for all strings being compared. A common mistake is to iterate based on one string's length without checking if shorter strings have characters at that position.