Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
Approach1: Vertical Scanning
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
for i, ch in enumerate(strs[0]):
for word in strs:
if i >= len(word) or word[i] != ch:
return word[:i]
return strs[0]Java Version of Vertical Scanning
Approach2: Horizontal Scanning
Java Version of Vertical Scanning
Last updated