Java Solution by comparing 2 strings at a time


#1

Open for suggestions, let me know if you think it needs any improvements.
public class Solution {
public String longestCommonPrefix(String[] A) {
int i,j=0,m,n=A.length,minlen=Integer.MAX_VALUE;
if(n==1){
return A[0];
}
for(i=0;i<n-1;i++){
String s1 = A[i];
String s2 = A[i+1];
j=0;
m = Math.min(s1.length(),s2.length());
while(j<m && s1.charAt(j)==s2.charAt(j)){
j++;
}
minlen = Math.min(minlen,j);
}
String ans = “”;
for(i=0;i<minlen;i++){
ans+=A[0].charAt(i);
}
return ans;
}
}