Really Simple Java Solution.


  • 0
    E
    public class Solution {
        public String longestCommonPrefix(String[] strs) {
            String res = "";
            if (strs.length == 0) return res;
            
            for (int i = 0; i < strs[0].length(); i++) {
                try {
                    char c = strs[0].charAt(i);
                    for (int j = 1; j < strs.length; j++) {
                        if (c != strs[j].charAt(i))
                            return res;
                    }
                    res = res + c;
                }
                catch (Exception e) {
                    return res;
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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