Solving the Longest Common Prefix Problem with C++ and Python

Photo by Denise Jans on Unsplash

Solving the Longest Common Prefix Problem with C++ and Python

·

3 min read

The Longest Common Prefix problem requires finding the longest common prefix string among an array of strings. If there is no common prefix, the function should return an empty string. This problem can be solved efficiently using various algorithms and data structures. In this blog post, we will explore two solutions to this problem using C++ and Python.

C++ Solution

The C++ solution to this problem involves comparing the first string in the array with all the other strings. For each string, we compare the characters in the same position until we find a mismatch. The characters that match in all the strings form the longest common prefix. If we find a mismatch, we break out of the loop and return the prefix.

#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string res = strs[0];
        int len = res.length();
        for(auto i=0; i< strs.size(); i++){
            while(strs[i].find(res)!=0){
                res= res.substr(0,len-1);
                len--;

                if(res.empty()){
                    return "";
                }
            }
        }
        return res;

        return "";
    }
};

Python Solution

The Python solution to this problem is similar to the C++ solution. We start by comparing the first string in the array with all the other strings. For each string, we compare the characters in the same position until we find a mismatch. The characters that match in all the strings form the longest common prefix. If we find a mismatch, we break out of the loop and return the prefix.

class Solution:
    def longestCommonPrefix(self, strs: list[str]) -> str:
        if len(strs) == 0:
            return ""
        else:
            s1, s2 = max(strs), min(strs)
            i, match = 0, 0
            while i < len(s1) and i < len(s2) and s1[i] == s2[i]:
                i, match = i+1, match + 1
            return s1[0:match]

The time complexity of this solution is O(N*M), where N is the number of strings in the vector and M is the length of the shortest string in the vector. This is because, in the worst-case scenario, every character of every string in the vector will be compared with every character of the common prefix string.

Therefore, this solution may not be very efficient for large arrays of strings, as it could take a long time to compare all the characters in all the strings. There are other more efficient algorithms, such as the Trie or Binary Search algorithms, that can solve this problem with lower time complexity.

Conclusion

In this blog post, we explored two solutions to the Longest Common Prefix problem using C++ and Python. Both solutions involve comparing the first string in the array with all the other strings to find the longest common prefix. The C++ solution uses nested loops, while the Python solution uses nested for-loops. The Python solution uses list indexing, while the C++ solution uses the string substr method.