Simple JAVA solution

  • 2

    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++;
            return s;

Log in to reply

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