Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 604 Bytes

0014. Longest Common Prefix.md

File metadata and controls

22 lines (17 loc) · 604 Bytes

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 "".

Screen Shot 2021-09-24 at 9 02 44 PM

/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function(strs) {
    for(let i = 0; i < strs[0].length; i++) {
        for(let str of strs) {
            if(str[i] !== strs[0][i]) return str.slice(0, i);
        }
    }
    return strs[0];
};