Problem statement

https://binarysearch.com/problems/Longest-Common-Prefix/

Solution

Equal to Leetcode 0014. Longest Common Prefix.

Complexity

Time complexity is O(S), where S is sum of lengths of all strings, space complexity is O(m), where m is maximal length of string.

Code

class Solution:
    def solve(self, strs):
        if not strs: return ""
        for i in range(len(strs[0]) + 1):
            if not all(i < len(word) and word[i] == strs[0][i] for word in strs):
                break
                
        return strs[0][:i]