# LeetCode Longest Word in Dictionary through Deleting

LeetCode Longest Word in Dictionary through Deleting

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

Example 1:

```Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]

Output:
"apple"
```

Example 2:

```Input:
s = "abpcplea", d = ["a","b","c"]

Output:
"a"
```

Note:

1. All the strings in the input will only contain lower-case letters.
2. The size of the dictionary won't exceed 1,000.
3. The length of all the strings in the input won't exceed 1,000.

```bool comp(const string& s1, const string& s2) {
return s1.size() > s2.size() || (s1.size() == s2.size() && s1 < s2);
}

class Solution {
private:
bool convert(const string& src, const string& dest){
int m = src.size(), n = dest.size();
if (m < n)return false;
int i = 0, j = 0;
while (i < m) {
while (i < m && src[i] != dest[j])++i;
if (i >= m)break;
++i;
++j;
if (j == n)break;
}
return j == n;
}
public:
string findLongestWord(string s, vector<string>& d) {
vector<string> cand;
for (int i = 0; i < d.size(); ++i) {
if (convert(s, d[i]))cand.push_back(d[i]);
}
sort(cand.begin(), cand.end(), comp);
if (cand.size() > 0)return cand[0];
else return "";
}
};
```