Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Longest Common Prefix

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



string longestCommonPrefix(vector<string>& strs) {
    int n = strs.size();
    if (n < 1)
	    return "";
    int m = strs[0].size();
    int i,j;
    for (i = 0; i < m; ++i) {
	    for (auto s : strs) {
		    if (i >= s.size() || s[i] != strs[0][i])
			    return strs[0].substr(0, i);
    return strs[0];
You can’t perform that action at this time.