Skip to content

Instantly share code, notes, and snippets.

@bunnyadad
Created April 15, 2019 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunnyadad/3d9885747ca6c2585c4c3f9bf055b371 to your computer and use it in GitHub Desktop.
Save bunnyadad/3d9885747ca6c2585c4c3f9bf055b371 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
class Solution {
public:
string longestCommonPrefix(vector<string> strs)
{
if (strs.empty()) return "";
string prefix = strs[0];
for (auto& str : strs)
{
while (!prefix.empty())
{
if (str.find(prefix) != 0)
{
prefix.pop_back();
}
else
{
break;
}
}
}
return prefix;
}
};
int main()
{
Solution a;
auto b = a.longestCommonPrefix({ "a", "acc", "acc" });
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment