Algorithm——Longest Common Prefix

Q:

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

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

A:

JAVASCRIPT

/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function(strs) {
    if (!strs.length) return '';
    if (strs.length == 1) return strs[0];
    
    var minest = 0;
    for (var i = 0; i < strs.length - 1; i++) {
        minest = Math.min(strs[i].length, strs[i+1].length);
    }

    var index = 0, longest = 0;
    while(index < minest) {
        for (var i = 0; i < strs.length - 1; i++) {

            if (strs[i][index] !== strs[i+1][index]) {
                longest = index;

                i = strs.length - 1
                index = minest
            } else {
                longest = index + 1;
            }
        }
        index++;
    }

    return strs[0].slice(0,longest);  
};

Due to the pursuit of low complexity, so think of this question for a long time. Originally quite frustrating, but the first time to see the speed reached top11%, or less happy about -

However, memory usage, or more, so there is much room for optimization. . . .

 

Reproduced in: https: //www.cnblogs.com/bbcfive/p/11066862.html

Guess you like

Origin blog.csdn.net/weixin_34326558/article/details/94451566