-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcp.py
34 lines (23 loc) · 777 Bytes
/
lcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import Literal
def lcp(a) -> None:
"""
Finds the longest common prefix (LCP) in a list of strings.
If no common prefix is found, returns an empty string.
:param a: A list of strings to find the LCP in
:type a: list[str]
:return: The longest common prefix in the list of strings
:rtype: str
"""
prefixes: dict = {}
for word in a:
prefix: Literal[""] = ""
for char in word:
prefix += char
prefixes.setdefault(prefix, 0)
prefixes[prefix] += 1
prefixesMax = max(prefixes.values())
if prefixesMax < len(a):
return ""
result = max((k for k, v in prefixes.items() if v == prefixesMax), key=len)
return result
lcp(["abc", "abcc", "abcd", "bcd"])