Problem 15: Longest Common Prefix

Python Developer | Audio Editor | Technical Writer | OSS Contributor | Tag Moderator @ @ThePracticalDEV | Valorant TonyPoppins #881488
Search for a command to run...

Python Developer | Audio Editor | Technical Writer | OSS Contributor | Tag Moderator @ @ThePracticalDEV | Valorant TonyPoppins #881488
No comments yet. Be the first to comment.
A no-fluff deployment runbook for getting a Cookiecutter Django project live on DigitalOcean using Docker and Traefik. Covers the full path from droplet provisioning to a working production deployment

If you’re switching between networks with different trust levels—home, café, coworking space—you probably don’t want your VPN behavior to be static. This guide walks through a clean, system-level way

Why this script exists If you've ever watched your free disk space quietly shrink over a few weeks of active development, you know the feeling: yesterday you had plenty of headroom, today your IDE is

A message from a colleague dropped into my inbox one morning: Hi Ice, now that we pushed the changes sa site, we need to scour the site for mentions of Level 1, Level 2, Level 1/2 and change them acc

Backing up your WordPress site is one of the most important maintenance tasks you can do as a site owner. While plugins like UpdraftPlus or Jetpack make this easy, knowing how to do it manually via SS

Hey everyone! 👋
Today, we're solving a popular string manipulation problem: Longest Common Prefix.
The goal is to write a function that finds the longest common prefix among a list of strings.
A prefix is a substring that occurs at the beginning of a string.
The function should return the longest common string that all words in the list start with.
If there is no common prefix, it should return an empty string "".
Examples:
longest_common_prefix(['flower', 'flow', 'flight']) → Should return 'fl'
longest_common_prefix(['dog', 'racecar', 'car']) → Should return "" (no common prefix)
longest_common_prefix(['interspecies', 'interstellar']) → Should return 'inters'
Here is the Python implementation:
def longest_common_prefix(strings):
"""
Finds the longest common prefix among a list of strings.
"""
# Check for empty or invalid input
if not strings:
return ""
# Set the first string as the initial prefix
prefix = strings[0]
# Iterate through the remaining strings
for s in strings[1:]:
# Shorten the prefix until it matches the start of the current string
while not s.startswith(prefix):
prefix = prefix[:-1]
# If the prefix becomes empty, there is no common prefix
if prefix == "":
return ""
return prefix
# Test cases
print(longest_common_prefix(['flower', 'flow', 'flight'])) # 'fl'
print(longest_common_prefix(['dog', 'racecar', 'car'])) # ''
print(longest_common_prefix(['interspecies', 'interstellar'])) # 'inters'
Let's walk through the code line by line:
def longest_common_prefix(strings):
longest_common_prefix that takes a list of words (strings) as input.if not strings:
Checks if the input list is empty or None.
If it is empty, we return an empty string "".
Note: This is a safer and more Pythonic check than strings is None or strings == 0 limit cases!
prefix = strings[0]
for s in strings[1:]:
Iterates through each subsequent string (s) in the list.
strings[1:] is Python slice notation that means "all elements from index 1 to the end".
while not s.startswith(prefix):
Keeps running as long as the current string s doesn't start with our assumed prefix.
.startswith() checks if a string begins with the specified prefix.
prefix = prefix[:-1]
Removes the last character from the prefix to make it shorter.
prefix[:-1] is string slicing that takes all characters except the last one.
if prefix == "":
If we've removed all characters from the prefix, it means the current word shares no common letters at the beginning with our initial assumed prefix.
We immediately return "" as we know there is no common sequence.
return prefix
Let's trace the function with longest_common_prefix(['flower', 'flow', 'flight']):
Initialization:
prefix = 'flower'Compare with 'flow' (First iteration):
'flow' doesn't start with 'flower'? True.
Remove last char: prefix = 'flowe'
'flow' doesn't start with 'flowe'? True.
Remove last char: prefix = 'flow'
'flow' doesn't start with 'flow'? False. Stop while loop.
Current prefix is safely 'flow'.
Compare with 'flight' (Second iteration):
'flight' doesn't start with 'flow'? True.
Remove last char: prefix = 'flo'
'flight' doesn't start with 'flo'? True.
Remove last char: prefix = 'fl'
'flight' doesn't start with 'fl'? False. Stop while loop.
Current prefix is safely 'fl'.
Result: Loop completes, return 'fl'. ✅
Now let's try longest_common_prefix(['dog', 'racecar', 'car']):
Initialization:
prefix = 'dog'Compare with 'racecar':
'racecar' doesn't start with 'dog'? True. Shorten to prefix = 'do'
'racecar' doesn't start with 'do'? True. Shorten to prefix = 'd'
'racecar' doesn't start with 'd'? True. Shorten to prefix = ""
Result: Prefix is empty "". Immediate return "". ❌ No common prefix found.
This approach relies heavily on Horizontal Matching:
Shrinking Search Pattern: Instead of checking letter by letter precisely (vertical scaling index-by-index), we start by assuming the maximum possible prefix and aggressively peel off letters from the end whenever mismatches are found.
Early Escape: If the prefix shrinks to an empty string at any point, we completely abort without iterating through the rest of the array strings.
This pattern is highly effective, easy to read, and simple to reason out! 🚀
Happy coding! 💻