Java solution, 1ms


  • 0
    X
    public static String longestCommonPrefix(String[] strs) {
            String result = "";
            if (strs == null || strs.length <= 0){
                return result;
            }
            int minSize = strs[0].length();
            int minIndex = 0;
    
            for (int i = 1; i < strs.length; i ++){
                if (strs[i].length() < minSize){
                    minSize = strs[i].length();
                    minIndex = i;
    
                }
            }
    
            for (int i = 0; i < strs.length; i ++){
                while (! strs[i].substring(0,minSize).equals(strs[minIndex].substring(0,minSize))){
                    minSize --;
                }
                result = strs[minIndex].substring(0,minSize);
            }
    
            return result;
    
        }```

Log in to reply
 

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