Simple JAVA solution


  • 2
    E

    Simple solution!

    public class Solution {
        public String longestCommonPrefix(String[] strs) {
            if(strs.length==0 || strs[0].length()==0) return "";
            String s = strs[0];
            for(int i=1;i<strs.length;i++){
                if(strs[i].length()==0) return "";
                int j=0;
                while(j<s.length() && j<strs[i].length() && strs[i].charAt(j)==s.charAt(j)) j++;
                s=s.substring(0,j);
            }
            return s;
        }
    }

Log in to reply
 

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