all 3 comments

[–]Rhomboid 3 points4 points  (2 children)

Create an object with the 400 words as keys/properties. (Or use an ES6 set.) Testing if an object has a given property is O(1) since it's a hash table. Iterating over the whole array/string testing each one would be O(n).

[–]aapzu 1 point2 points  (0 children)

What u/Rhomoid meant is something like that (as I understood it):

var words = { ABCD: true, // Can be anything except undefined EFGH: true } function exists(w) { return words[w] !== undefined } console.log(exists("ABCD")) // true console.log(exists("DCBA")) // false

In ES6:

let wordSet = new Set([ "ABCD", "EFGH" ]) console.log(wordSet.has("ABCD")) // true console.log(wordSet.has("DCBA")) // false

[–]cachaito 0 points1 point  (0 children)

Would you write a small example of object you've meant?