leetcode

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

0522.cpp (736B)


      1 class Solution {
      2   public:
      3     int findLUSlength(vector<string> &strs) const {
      4         static const auto LCS = [](const string &a, const string &b) {
      5             int i = 0, j = 0;
      6             while (i < size(a) && j < size(b)) {
      7                 if (a[i] == b[j]) i++;
      8                 j++;
      9             }
     10 
     11             return i == size(a);
     12         };
     13 
     14         sort(begin(strs), end(strs), [](const string &a, const string &b) { return size(a) > size(b); });
     15 
     16         const int n = size(strs);
     17         for (int i = 0; i < n; i++) {
     18             for (int j = 0; j < n; j++) {
     19                 if (i != j && LCS(strs[i], strs[j])) goto next;
     20             }
     21             return size(strs[i]);
     22         next:;
     23         }
     24 
     25         return -1;
     26     }
     27 };