14. Longest Common Prefix
Vertical search
O(n)
from typing import List

class Solution:
    def longestCommonPrefix(self, strs : List[str]) -> str:
        if len(strs) == 0:
            return ''

        if len(strs) == 1:
            return strs[0]

        re = ''
        size = len(strs[0])
        for e in range(len(strs)-1):
            if len(strs[e+1]) < size:
                size = len(strs[e+1])

        for i in range(size):
            letter = strs[0][i]
            for e in range(len(strs)-1):
                if letter != strs[e+1][i]:
                    return re
            re += letter

        return re

def main():
    s = Solution()

    print(s.longestCommonPrefix(["flower","flow","flight"])) # "fl"
    print(s.longestCommonPrefix(["dog","racecar","car"])) # ""

if __name__ == '__main__':
    main()
			
from typing import List

class Solution:
    def longestCommonPrefix(self, strs : List[str]) -> str:
        if len(strs) == 0:
            return ""

        length = len(strs[0])

        for i in range(length):
            for s in strs:
                if i == len(s) or s[i] != strs[0][i]:
                    return s[:i]
        
	return strs[0]

def main():
    s = Solution()

    print(s.longestCommonPrefix(["flower","flow","flight"])) # "fl"
    print(s.longestCommonPrefix(["dog","racecar","car"])) # ""

if __name__ == '__main__':
    main()
			
Horizontal scanning
O(n)
from typing import List

class Solution:
    def longestCommonPrefix(self, strs : List[str]) -> str:
        if len(strs) == 0:
            return ""

        common = strs[0]

        for i in range(1, len(strs)):
            while strs[i].find(common) != 0:
                common = common[:len(common)-1]

        return common

def main():
    s = Solution()

    print(s.longestCommonPrefix(["flower","flow","flight"])) # "fl"
    print(s.longestCommonPrefix(["dog","racecar","car"])) # ""

if __name__ == '__main__':
    main()