[LeetCode-String] The longest common prefix

Title description

Write a function to find the longest common prefix in a string array.
If there is no public prefix, the empty string "" is returned.
Examples

输入: ["flower","flow","flight"]
输出: "fl"

输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。

Title link: https://leetcode-cn.com/problems/longest-common-prefix/

Idea 1

Find the shortest string first, then traverse the shortest string, and compare the current bit with the rest of the string: if the current bit of the remaining string and the shortest string are all equal, then compare the next bit; if there is one If they are not equal, the loop terminates, and the previous digit from the beginning of the string to the current position is the longest common prefix (in fact, you don't need to compare based on the shortest string, just find a string as a baseline comparison). code show as below:

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.empty()) return "";

        int shortest = 0x7fffffff;
        string shortestStr = "";
        for(int i=0; i<strs.size(); i++){
            if(strs[i].length()<shortest){
                shortest = strs[i].length();
                shortestStr = strs[i];
            }
        }

        int i=0;
        while(i<shortest){
            bool breakFlag = false;
            for(int j=0; j<strs.size(); j++){
                if(strs[j][i]!=shortestStr[i]){
                    breakFlag = true;
                    break;
                }
            }
            if(breakFlag) break;
            i++;
        }
        return shortestStr.substr(0,i);
    }
};
  • Time complexity: O (n)
    n is the length of the string array. The worst case is O (n * m), where m is the length of the shortest string.
  • Space complexity: O (1)

Idea 2

First select a string as the current prefix, you may choose the first one, and then determine whether the current prefix is ​​the prefix of the second string: if not, the last character of the current prefix is ​​removed, if the current prefix is If it is empty, it returns directly; if it is not empty and is the prefix of the second string; then continue to compare the third string by the same method until the comparison is completed or the prefix is ​​empty. If the comparison is completed, the current prefix If it is not empty, the current prefix is ​​the longest common prefix. code show as below:

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.empty()) return "";

        string prefix = strs[0];
        for(int i=1; i<strs.size(); i++){
            while(strs[i].find(prefix)!=0){    // prefix不是strs[i]的前缀
                prefix = prefix.substr(0, prefix.length()-1);
                if(prefix.empty()) return prefix;
            }
        }
        return prefix;
    }
};
  • Time complexity: O (n)
    n is the length of the string array.
  • Space complexity: O (1)

Guess you like

Origin www.cnblogs.com/flix/p/12686641.html