#Horizontal Scanning
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
prefix = strs[0]
for i in range(1,len(strs)): #O(n)
while(strs[i].find(prefix) != 0): #O(S)
prefix = prefix[0:len(prefix)-1]
if len(prefix) == 0:
return ""
return prefix
#Vertical Scanning
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
prefix = ""
for i in range(len(strs[0])):
c = strs[0][i]
for j in range(1,len(strs)):
if i >= len(strs[j]):
return prefix
if c != strs[j][i]:
return prefix
prefix += c
return prefix