java | Set | Easy |
\nclass Solution {\n    public String longestWord(String[] words) {\n        HashSet<String > h=new HashSet<>();\n        for(String s:words){\n            h.ad
```\nclass Solution {\n    public String longestWord(String[] words) {\n        HashSet<String > h=new HashSet<>();\n        for(String s:words){\n            h.add(s);\n        }\n        String res="";\n       for(String s:words){\n            if (s.length() > res.length() ||\n                    s.length() == res.length() && s.compareTo(res) < 0) {\n                boolean b=true;\n                for(int j=1;j<s.length();j++){\n                    if(!h.contains(s.substring(0,j))){\n                        b=false;\n                        break;\n                    }\n                }\n                if(b){\n                    res=s;\n                }\n            }\n        }\n        return res;\n    }\n}\n```
['Java']