Solution_in_Java


#1

public class Solution {
public String longestCommonPrefix(ArrayList A) {
if(A.size()==1)
return A.get(0);

    int len=A.size();
    int minl=Integer.MAX_VALUE;
    
    for(int i=0;i<len-1;i++)
    {
        String s1 = A.get(i);
        String s2 = A.get(i+1);
        int j=0;
        int m = Math.min(s1.length(),s2.length());
        
        while(j<m && s1.charAt(j)==s2.charAt(j))
            j++;
        
        minl = Math.min(minl,j);
    }
    
    String prefix = (A.get(0)).substring(0,minl);
    
    return prefix;
    
    

                
}

}