skip to Main Content

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


  1. 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…

    const Valid=['home','boTTle','glasses','GodKnows'];
    
    var S='y89nsdadhomea98qwoi'.toLowerCase(), Ok=false;
    
    for(var i=0; i<Valid.length; i++){
    
     if(S.includes(Valid[i].toLowerCase())){Ok=true; break;}
    
    }
    
    
    if(Ok){
    
     // yeah the string is ok, what now?
    
    }
    

    Sorry I’ve had a few scotches and LMAO after reading your post. 🙂

    Login or Signup to reply.
  2. 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:

    1. Build an ahead-of-time special dictionary hashmap, this costs you O(m) (which m is the number of your dictionary words). The hashmap will be something like:
    // As object is hashmap-like in javascript
    dictionaryMap = {
       'hom': 'e',
       'cat': '',
       'bot': 'tle',
       'gla': ['ss', 'cier'], // contains 'glass' and 'glacier'
    };
    
    1. Iterate over every characters in the source string and look for the word, that way, you have O(n) time for the iteration and O(1) for the lookup:
    for (i=0; i<n.length; i++) {
       lookupStr := n[i] + n[i+1] + n[i+2]; // <-- I know it's dump, just a sample :)))
       if (dictionaryMap.hasOwnProperty(lookupStr) {
          console.log(lookupStr + dictionaryMap[lookupStr])
          return 'hell yeah';
       }
    }
    
    1. As now you know for sure that the source string has a high chance contains an English word larger than 3, you can apply dynamic programming, simple string check or change the dictionaryMap and do step 2 recursion if you want to look for an exact word:
    dictionaryMap = {
       'gla': 'ss|cier'
    }
    // Apply dynamic programming or memoization to find the longest continuously common subsequence...
    

    OR

    dictionaryMap = {
       'gla': {
          's': {'s': ''},
          'c': {'i': {'e': {'r': ''}}}
       }
    // Continue doing Step 2...
    

    => Total: O(m) + O(n) + O(1) = O(m) time complexity and O(m) or O(m*longestWordCharacters) space complexity

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search