Easy String String

Longest Common Prefix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def longest_common_prefix(words) -> "str":
    if not words:
        return ""

    shortest_word = min(words, key=len)

    for i, letter in enumerate(shortest_word):
        for s in words:
            if s[i] != letter:
                return shortest_word[:i]

    return shortest_word


assert longest_common_prefix(["flower", "flow", "flight"]) == "fl"

🎰