Longest Common Prefix
Total Accepted: 44491 Total Submissions: 170999 My Submissions
Question Solution
Write a function to find the longest common prefix string amongst an array of strings.
Show Tags
Parsing, the longest common prefix, first finds the shortest string length, as a metric size, and then sequentially parses each character to see if the characters are the same, and the same is added to the final result set, different stops, returns the final result
public class Solution {
Public String Longestcommonprefix (string[] strs) {
int count=strs.length;
String nullstr= "";
if (count==0)
return nullstr;
if (count==1)
return strs[0];
int Ml=strs[0].length ();
for (int i=1;i<count;i++)
{
int L=strs[i].length ();
if (L<ML)
Ml=l;
}
if (ml==0)
return nullstr;
Else
{
String s= "";
int j=0;
for (int i=0;i<ml;i++)
{
for (j=1;j<count;j++)
if (Strs[j].charat (i)!=strs[j-1].charat (i))
Break
if (J!=count)
Break
Else
S=s+strs[j-1].charat (i);
}
return s;
}
}
}
Leetcode#14longest Common Prefix