14 lines 1ms easy to understand JAVA solution


  • 0
    M
    public class Solution {
        public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0) return "";
        String CPrefix = strs[0];
        for(int i = 0; i < strs.length; i++) {
            String curString = strs[i];
            for(int j = CPrefix.length()-1; j >= 0; j--) {
                if(curString.startsWith(CPrefix)) break;
                CPrefix = CPrefix.substring(0,j);
            }
        }
        return CPrefix;
        }
    }

Log in to reply
 

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