Fast and Short JAVA solution easy to understand


  • -1
    W
    public class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs == null || strs.length == 0) return "";
            int len = 0;
            for (int i = 0; i < strs[0].length(); i++){
                char c = strs[0].charAt(i);
                for (String str : strs){
                    if (i >= str.length() || str.charAt(i) != c) return strs[0].substring(0, len);
                }
                len++;
            }
            return strs[0].substring(0, len);
        }
    }

Log in to reply
 

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