My Solution - any comments


  • 0
    A
    public class Solution {
        public String longestCommonPrefix(String[] strs) {
            if(strs == null || strs.length==0)
                return new String();
            String minSize = strs[0];
            for(int i=1;i<strs.length;i++)
                if(strs[i].length() < minSize.length())
                    minSize = strs[i];
            
            int n = minSize.length();
            for(int i=0;i<strs.length && n!=0;i++){
                if(!strs[i].substring(0,n).equals(minSize.substring(0,n))){
                    n--;
                    i--;
                }
            }
            
            if(n == 0)
                return "";
            return minSize.substring(0,n);
            
            
            
            
        }
    }
    

    Calculating the min first in the array and then comparing


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.