Skip to content

Latest commit

 

History

History

longest-word-in-dictionary

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

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