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)


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