From b0bcc23cca3ba4dffeeda174caa228cbffec97c4 Mon Sep 17 00:00:00 2001 From: Gabriel Donnan <47415809+gabedonnan@users.noreply.github.com> Date: Sat, 14 Jan 2023 00:17:25 +0000 Subject: [PATCH] Create longest-common-pref.py neat enough solution for longest common prefix --- longest-common-pref.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 longest-common-pref.py diff --git a/longest-common-pref.py b/longest-common-pref.py new file mode 100644 index 0000000..aa808be --- /dev/null +++ b/longest-common-pref.py @@ -0,0 +1,13 @@ +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + pref = strs[0] + for s in strs: + if pref == "": + return pref + for i in range(min(len(s),len(pref))): + if s[i] != pref[i]: + pref = pref[:i] + break + if len(pref) > len(s): + pref = pref[:len(s)] + return pref