DEV Community


Posted on

LeetCode 14. Longest Common Prefix (javascript solution)


Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".


Time Complexity : O(n^2)
Space Complexity: O(1)

var longestCommonPrefix = function(strs) {
    if (strs.length == 0) return "";
    let prefix = strs[0];
    for (let i = 1; i < strs.length; i++)
        // Trim the prefix until it fits in the front of the current word
        while (strs[i].indexOf(prefix) !== 0) {
            prefix = prefix.substring(0, prefix.length - 1);
            // If we cannot trim down the prfix to fit in the word then we have no common prefix amoung words in str
            if (prefix.length===0) return "";
    return prefix;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)