Python: Longest common prefix of all the strings
23. Find Longest Common Prefix of All Strings Using a Set
Write a Python program to find the longest common prefix of all strings. Use the Python set.
Sample Solution:
Python Code:
# Define a function 'longest_Common_Prefix' that takes a list of strings 'strs' as input.
def longest_Common_Prefix(strs):
    # Check if the input list 'strs' is empty, and return an empty string if so.
    if not strs:
        return ""
    
    # Find the minimum length of strings in the list using a list comprehension.
    min_length = min([len(word) for word in strs])
    
    # Iterate from the first character to the 'min_length'.
    for i in range(min_length):
        # Create a set 'chars' containing the i-th character of each string in 'strs'.
        chars = set([word[i] for word in strs])
        
        # If the set 'chars' has more than one element, return the common prefix found so far (up to the i-th character).
        if len(chars) > 1:
            return strs[0][:i]
    
    # If no common prefix was found in the loop, return the common prefix as the first string up to the 'min_length'.
    return strs[0][:min_length] 
# Define a list of strings 'strs' for testing.
strs = ["pqrefgh", "pqrsfgh"]
print("Original list of strings:")
print(strs)
# Call the 'longest_Common_Prefix' function and print the result for the list of strings.
print("Longest common prefix of all said strings:")
print(longest_Common_Prefix(strs))
# Repeat the process for different sets of strings.
strs = ["w3r", "w3resource"]
print("\nOriginal list of strings:")
print(strs)
print("Longest common prefix of all said strings:")
print(longest_Common_Prefix(strs))
strs = ["Python", "PHP", "Perl"]
print("\nOriginal list of strings:")
print(strs)
print("Longest common prefix of all said strings:")
print(longest_Common_Prefix(strs))
strs = ["Python", "HTML", "PHP"]
print("\nOriginal list of strings:")
print(strs)
print("Longest common prefix of all said strings:")
print(longest_Common_Prefix(strs))
Sample Output:
Original list of strings: ['pqrefgh', 'pqrsfgh'] Longest common prefix of all said strings: pqr Original list of strings: ['w3r', 'w3resource'] Longest common prefix of all said strings: w3r Original list of strings: ['Python', 'PHP', 'Perl'] Longest common prefix of all said strings: P Original list of strings: ['Python', 'HTML', 'PHP'] Longest common prefix of all said strings:
Flowchart:
 
For more Practice: Solve these Related Problems:
- Write a Python program to determine the longest common prefix among a list of strings by converting the first few characters into a set and checking for uniqueness.
- Write a Python program to use set intersection on the characters of each string to compute the longest common starting substring.
- Write a Python program to iterate through characters of the first string and compare with others, utilizing a set to track mismatches.
- Write a Python program to implement a function that returns the longest common prefix of a list of strings, and print "No common characters" if none exists.
Go to:
Previous:  Find all pairs in a list whose sum is equal to a target value.
Next:  Maximum product of two numbers among all pairs in a list.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
