leetcode 14 Longest Common Prefix
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty())
return "";
int size = strs.size();
string prefix = strs[0];
for (int i = 1; i < size; i++){
if (prefix.empty() || strs[i].empty())
return "";
int steplength = max(prefix.length(), strs[i].length());
int j = 0;
for (j; j < steplength; j++)
{
if (prefix[j] != strs[i][j])
break;
}
prefix = prefix.substr(0, j);
}
return prefix;
}
};