We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 424cdbb commit 875102cCopy full SHA for 875102c
General Questions/Longest_Common_Prefix.py
@@ -0,0 +1,21 @@
1
+#Longest Common Prefix in python
2
+
3
+#define the function to evaluate the longest common prefix
4
+def longestCommonPrefix(s):
5
+ p = '' #declare an empty string
6
+ for i in range(len(min(s, key=len))):
7
+ f = s[0][i]
8
+ for j in s[1:]:
9
+ if j[i] != f:
10
+ return p
11
+ p += f
12
+ return p #return the longest common prefix
13
14
15
+n = int(input("Enter the number of names in list for input:"))
16
+print("Enter the Strings:")
17
+s = [input() for i in range(n)]
18
+if(longestCommonPrefix(s)):
19
+ print("The Common Prefix is:" ,longestCommonPrefix(s))
20
+else:
21
+ print("There is no common prefix for the given list of strings, hence the answer is:", 0)
0 commit comments