From cf6ef72f9bee13a5e93ca86aa6150720a30ead6d Mon Sep 17 00:00:00 2001 From: sindz22 Date: Thu, 9 Oct 2025 18:14:32 +0530 Subject: [PATCH 1/3] strings: add longest_common_prefix algorithm with doctests --- strings/longest_common_prefix.py | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 strings/longest_common_prefix.py diff --git a/strings/longest_common_prefix.py b/strings/longest_common_prefix.py new file mode 100644 index 000000000000..a768895f0b0a --- /dev/null +++ b/strings/longest_common_prefix.py @@ -0,0 +1,48 @@ +def longest_common_prefix(strs: list[str])->str: + """ + :type strs: List[str] + + Args: + strs (list[str]): A list of strings. + + + Returns: + str:The longest common prefix shared among all strings. + Returns an empty string if there is none. + + Test cases: + >>> longest_common_prefix(["flower","flow","flap"]) + 'fl' + >>> longest_common_prefix(["cats","dragon","dracula"]) + '' + >>> longest_common_prefix(["marianne","mariachi"]) + 'maria' + >>> longest_common_prefix([]) + '' + >>> longest_common_prefix(["","belly","belt"]) + '' + >>> longest_common_prefix(["#hashtag","#hashbrown","#hash"]) + '#hash' + """ + ans="" + if not strs: + return "" + if len(strs)==1: + return strs[0] + + k=0 + ref=strs[0] + + while k Date: Thu, 9 Oct 2025 18:22:10 +0530 Subject: [PATCH 2/3] strings: add Wikipedia link in docstring for longest_common_prefix --- strings/longest_common_prefix.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/strings/longest_common_prefix.py b/strings/longest_common_prefix.py index a768895f0b0a..f70e2873f0b8 100644 --- a/strings/longest_common_prefix.py +++ b/strings/longest_common_prefix.py @@ -2,6 +2,9 @@ def longest_common_prefix(strs: list[str])->str: """ :type strs: List[str] + URL: + https://en.wikipedia.org/wiki/Longest_common_prefix + Args: strs (list[str]): A list of strings. From b4e7e9a0778ec02360a888e1d6a26605eceadc29 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Oct 2025 12:58:03 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/longest_common_prefix.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/strings/longest_common_prefix.py b/strings/longest_common_prefix.py index f70e2873f0b8..4056804e16c4 100644 --- a/strings/longest_common_prefix.py +++ b/strings/longest_common_prefix.py @@ -1,7 +1,7 @@ -def longest_common_prefix(strs: list[str])->str: +def longest_common_prefix(strs: list[str]) -> str: """ :type strs: List[str] - + URL: https://en.wikipedia.org/wiki/Longest_common_prefix @@ -12,7 +12,7 @@ def longest_common_prefix(strs: list[str])->str: Returns: str:The longest common prefix shared among all strings. Returns an empty string if there is none. - + Test cases: >>> longest_common_prefix(["flower","flow","flap"]) 'fl' @@ -27,25 +27,19 @@ def longest_common_prefix(strs: list[str])->str: >>> longest_common_prefix(["#hashtag","#hashbrown","#hash"]) '#hash' """ - ans="" + ans = "" if not strs: return "" - if len(strs)==1: + if len(strs) == 1: return strs[0] - - k=0 - ref=strs[0] - while k