Create the longest common prefix with the direct neighbour
Solution Breakdown
Example of looking at differences
samplePseudocode
# INSERT PSEUDOCODE HEREPython Solution
def longestCommonPrefix(words: list[str]) -> str:
# Base Case: list of words only has 1 value
if len(words) == 1:
return words[0]
prefix = words[0]
for i in range(1, len(words)):
neighbour = words[i]
while not neighbour.startswith(prefix):
prefix = prefix[:len(prefix)-1]
if prefix == "":
return ""
# end of while
# end of for
return prefix
# end of longestCommonPrefix()Code Explanation
Connected Readings
Last updated