---
# System prepended metadata

title: 524. Longest Word in Dictionary through Deleting
tags: [LeetCode]

---

# 524. Longest Word in Dictionary through Deleting
###### tags: `LeetCode`
## **Link**
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/
## **Code**
```cpp=
class Solution {
public:
    string findLongestWord(string s, vector<string>& dictionary) 
    {
        set<string> ans; // 解答的集合
        int mx=-1;
        for(int i=0;i<dictionary.size();++i)
        {
            for(int j=0,k=0;j<s.size();++j)
            {
                if(s[j]==dictionary[i][k])
                    k++;
                if(k==dictionary[i].size()) // 全部字母都可以找到的話
                {
                    if(k>mx) // 比目前最長的解答還長
                    {
                        ans.clear(); // 之前的全清掉
                        mx=k; // 更新最長的長度
                        ans.insert(dictionary[i]);
                    }
                    else if(k==mx) // 和現在的解答一樣長
                        ans.insert(dictionary[i]);
                    break;
                }
            }
        }
        if(ans.empty())
            return "";
        else
            return *(ans.begin());
    }
};
```
## date
**2023.01.20**

{%hackmd @nnks8908/background_leetcode %}