leetcode-study

14. Longest Common Prefix

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        """
        Find the longest common prefix string among an array of strings.
        
        Parameters:
            strs (List[str]): The list of strings to analyze.
            
        Returns:
            str: The longest common prefix shared by the strings in the list.
                 If there is no common prefix, returns an empty string.
        """
        # If the list is empty, return an empty string immediately.
        if not strs:
            return ""
        
        # Start with the first string as the initial prefix candidate.
        prefix = strs[0]
        
        # Iterate over the remaining strings in the list.
        for s in strs[1:]:
            # Reduce the prefix until the current string starts with it.
            while not s.startswith(prefix):
                # Update the prefix by removing the last character.
                prefix = prefix[:-1]
                # If the prefix becomes empty, return an empty string.
                if not prefix:
                    return ""
                    
        # Return the longest common prefix after processing all strings.
        return prefix

Summary of Techniques and Approaches: