What is the best way in NodeJS to create a function that returns true iff a string contains an english word longer than 3 letters?
The code will be placed in a lambda, so I’m looking for the most efficient solution. The best solution I’ve got so far is to use dictionary-en
and iterate over every word calling .includes(word)
on the source string, I was wondering if you can think of better approach to implement this.
Some examples of strings which should return true:
- y89nsdadhomea98qwoi
- :_5678aSD.boTTleads.
- yfugdnuagybdasglassesmidwqihhniwqnhi
Some examples of strings which should return false:
- y89nsdadhasa98qwoi
- :_5678aSD.b0TTle4ds.
- yfugdnuagybdasmidwqihhniwqnhi
2
Answers
Huh? This is not a job for node.js but JavaScript!
What is node.js…
https://en.wikipedia.org/wiki/Node.js
Onto the problem at hand…
An English word greater than 3 letters… but there are tens of THOUSANDS of them!
Do you have a text file with all these words included so that we can load them into an array to operate?
No? OK, in the meantime here’s the best we’ve got…
Sorry I’ve had a few scotches and LMAO after reading your post. 🙂
Looping over a dictionary (which may contains hundred of thousand of words) is not a good idea. As your string ranges 10-200 chars, iterating over every characters in the source string gives a better result of time complexity. And if you don’t care about space complexity, there’s a better approach:
O(m)
(whichm
is the number of your dictionary words). The hashmap will be something like:OR
=> Total:
O(m) + O(n) + O(1) = O(m)
time complexity andO(m)
orO(m*longestWordCharacters)
space complexity