leetcode

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

0524.cpp (872B)


      1 class Solution {
      2   public:
      3     string findLongestWord(const string &s, const vector<string> &dictionary) const {
      4         static int count[1001][26];
      5         const int n = size(s);
      6         string res;
      7 
      8         memset(count[n], 0x00, sizeof(count[n]));
      9         for (int i = n - 1; i >= 0; i--) {
     10             memcpy(count[i], count[i + 1], sizeof(count[n]));
     11             count[i][s[i] - 'a'] = i + 1;
     12         }
     13 
     14         for (const auto &word : dictionary) {
     15             int pos = 0;
     16             for (const char c : word) {
     17                 const int idx = c - 'a';
     18                 if (!count[pos][idx]) goto next;
     19                 pos = count[pos][idx];
     20             }
     21 
     22             if (size(word) > size(res))
     23                 res = word;
     24             else if (size(word) == size(res))
     25                 res = min(res, word);
     26 
     27         next:;
     28         }
     29 
     30         return res;
     31     }
     32 };