Skip to content

Latest commit

 

History

History
34 lines (27 loc) · 793 Bytes

File metadata and controls

34 lines (27 loc) · 793 Bytes

Longest word in dictionary

Problem link

Solutions

Solution.cpp

// https://leetcode.com/problems/longest-word-in-dictionary

class Solution {
 public:
  string longestWord(vector<string>& words) {
    unordered_set<string> s;
    for (const auto& w : words) s.insert(w);

    string best = "";
    for (const auto& w : words) {
      if (w.size() < best.size() or (w.size() == best.size() and best < w))
        continue;
      for (int i = 1, L = w.size(); i <= L; ++i)
        if (!s.count(w.substr(0, i))) goto BPP;
      best = w;
    BPP:;
    }
    return best;
  }
};

Tags