Program to find longest common prefix from list of strings in Python

Suppose we have a list of lowercase strings, we have to find the longest common prefix.

So, if the input is like ["antivirus", "anticlockwise", "antigravity"], then the output will be "anti"

To solve this, we will follow these steps −

sort the list words alphabetically

  • prefix := a new list
  • flag := 0
  • for i in range 0 to size of words[0], do
    • for each j in words, do
      • if j[i] is not same as last element of prefix, then
        • delete last element from prefix
        • flag := 1
        • come out from the loop
    • if flag is same as 1, then
      • come out from the loop
  • return string after concatenating all elements present in prefix array

Let us see the following implementation to get better understanding −


 Live Demo

class Solution:
   def solve(self, words):
      prefix = []
      flag = 0
      for i in range(len(words[0])):
         for j in words:
            if j[i] != prefix[-1]:
               flag = 1
         if flag == 1:
         return ''.join(prefix)
ob = Solution()
words = ["antivirus", "anticlockwise", "antigravity"]


["antivirus", "anticlockwise", "antigravity"]



Updated on: 06-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started