GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #3136 - Valid Word - LeetCode JavaScript Solutions

Back to all solutions

#3136 - Valid Word

Easy • View on LeetCode • View on GitHub
String

Problem Description

A word is considered valid if:

  • It contains a minimum of 3 characters.
  • It contains only digits (0-9), and English letters (uppercase and lowercase).
  • It includes at least one vowel.
  • It includes at least one consonant.

You are given a string word.

Return true if word is valid, otherwise, return false.

Notes:

  • 'a', 'e', 'i', 'o', 'u', and their uppercases are vowels.
  • A consonant is an English letter that is not a vowel.

Solution

/**
 * @param {string} word
 * @return {boolean}
 */
var isValid = function(word) {
  if (word.length < 3) return false;

  const vowels = new Set('aeiouAEIOU');
  const special = new Set('@#$');
  const digits = new Set('0123456789');
  const wordSet = new Set(word);

  if (hasIntersection(wordSet, special)) return false;
  if (!hasIntersection(wordSet, vowels)) return false;

  const result = new Set([...wordSet].filter(char => {
    return !vowels.has(char) && !digits.has(char);
  }));

  return result.size > 0;

  function hasIntersection(set1, set2) {
    for (const item of set1) {
      if (set2.has(item)) return true;
    }
    return false;
  }
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.