Longest Common Prefix 2021-05-28 04:00

Problem Description

public static String longestCommonPrefix(String[] strs) {
    if (strs.length == 0) {
        return "";
    }
    if (strs.length == 1) {
        return strs[0];
    }
    String base = strs[0];
    for (String str : strs) {
        while (!str.startsWith(base)) {
            base = base.substring(0, base.length() - 1);
        }
    }
    return base;
}
Runtime Memory
0 ms 36.8 MB

henryxi leetcode list

EOF