Longest Common Prefix
Total Accepted: 107085
Total Submissions: 371911
Difficulty: Easy
Write a function to find the longest common prefix string amongst an array of strings.
public class Solution{
public String longestPreString(String[] s):
String pre=s[0];
int i=1;
while(i<s.length){
while(s[i].indexOf(pre)!=0){
pre=pre.subString(0,pre.length()-1);
}
i++;
}
return pre;
}
Python:
class Solution:
def longestPreString(self,s):
pre=s[0]
i=1
while i<len(s):
while s[i].index(pre)!=0:
pre=pre[0:len(s)-1]
i+=1
return pre