leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0014.cpp (427B)


      1 class Solution {
      2   public:
      3     string longestCommonPrefix(vector<string> &strs) {
      4         int mlen = 200;
      5         for (string &s : strs)
      6             mlen = min(mlen, (int)s.size());
      7 
      8         string res = "";
      9         for (int i = 0; i < mlen; i++) {
     10             for (int j = 1; j < strs.size(); j++)
     11                 if (strs[j][i] != strs[0][i]) return res;
     12             res += strs[0][i];
     13         }
     14         return res;
     15     }
     16 };