diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/longest_word_dict.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/longest_word_dict.cpp')
-rw-r--r-- | other/longest_word_dict.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/other/longest_word_dict.cpp b/other/longest_word_dict.cpp new file mode 100644 index 0000000..3be4538 --- /dev/null +++ b/other/longest_word_dict.cpp @@ -0,0 +1,31 @@ +#include "leetcode.h" + +class Solution { +public: + string findLongestWord(string s, vector<string> &dictionary) { + string ans; + for (auto d : dictionary) + if (canForm(d, s)) + if (d.size() > ans.size() || (d.size() == ans.size() && d < ans)) + ans = d; + return ans; + } + +private: + bool canForm(string w, string s) { + int w1 = 0, s1 = 0; + while (w1 < w.size() && s1 < s.size()) { + if (w[w1] == s[s1]) + w1++; + s1++; + } + return w1 == w.size(); + } +}; + +int main() { + Solution obj; + string s = "abpcplea"; + vector<string> dictionary = {"ale", "apple", "monkey", "plea"}; + cout << obj.findLongestWord(s, dictionary); +} |