Python Concise Code with Explanation


  • 0
    S

    zip - used for iterating over multiple lists.
    *strs - for making multiple lists out of a single list
    Hence, the loop will iterate over 1st character in all the elements then second and so on...

    A set of such a string will have a single character if its a common prefix across all the strings.
    Hence we can break from the loop once this trend ends.

    Hope the explanation helps!

            if not strs:
                return ""
            count=0
            for letter_group in zip(*strs):
                if(len(set(letter_group))>1):
                    break
                count = count + 1
            return(strs[0][0:count])
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.