Valid Word

Easy
1769.

Valid Word

Valid Word is an easy coding problem on strings, asked in Amazon and Google interviews.

A word is valid when all of the following hold:

  • it has at least 3 characters;
  • it contains only digits 0–9 and English letters (uppercase or lowercase);
  • it contains at least one vowel (a, e, i, o, u in either case);
  • it contains at least one consonant (any English letter that is not a vowel).

Given a string word, return true if it is valid, otherwise false.

Example 1

Input
word = "234Adas"
Output
true
Explanation
Seven characters, only digits and letters, with vowel 'A' and consonant 'd'.

Example 2

Input
word = "b3"
Output
false
Explanation
Fewer than three characters, and no vowel.

Example 3

Input
word = "a3$e"
Output
false
Explanation
'$' is neither a digit nor a letter.

Constraints

  • 1 <= word.length <= 20
  • word consists of English uppercase and lowercase letters, digits, '@', '#' and '$'
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} word
 * @return {boolean}
 */
var isValid = function(word) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Valid Word — Solution in Python, Java & C++

Valid Word solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at Amazon and Google.

Valid Word is an easy coding problem on strings, asked in Amazon and Google interviews.

  • Difficulty: Easy
  • Topics: Strings
  • Asked at: Amazon, Google
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

A word is valid when all of the following hold:

  • it has at least 3 characters;
  • it contains only digits 0–9 and English letters (uppercase or lowercase);
  • it contains at least one vowel (a, e, i, o, u in either case);
  • it contains at least one consonant (any English letter that is not a vowel).

Given a string word, return true if it is valid, otherwise false.

Example 1

Input: word = "234Adas"
Output: true
Explanation: Seven characters, only digits and letters, with vowel 'A' and consonant 'd'.

Example 2

Input: word = "b3"
Output: false
Explanation: Fewer than three characters, and no vowel.

Example 3

Input: word = "a3$e"
Output: false
Explanation: '$' is neither a digit nor a letter.

Constraints

  • 1 <= word.length <= 20
  • word consists of English uppercase and lowercase letters, digits, '@', '#' and '$'

Hints for Valid Word

  1. Hint 1

    Check the length first; then look at every character once.

  2. Hint 2

    A character that is neither a letter nor a digit makes the word invalid immediately.

  3. Hint 3

    Track two flags — saw a vowel, saw a consonant — and lowercase a letter before testing whether it is a vowel. Digits set neither flag.

How to solve Valid Word

Each rule is a property of single characters or of the length, so one pass with two flags checks them all.

Approach

  1. If word.length < 3, return false.
  2. For each character: if it is a letter, set vowel when its lowercase form is in aeiou, otherwise set consonant; if it is a digit, do nothing; otherwise return false.
  3. Return vowel && consonant.

Why it works

The pass rejects any forbidden character, and after it the two flags say exactly whether a vowel and a consonant occurred, which with the length check covers all four rules.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Uppercase vowels count as vowels: "AbC" is valid.
  • Digits are allowed but are not consonants — "a12" has no consonant.
  • Testing for 'not a vowel' without first checking it is a letter would count digits or symbols as consonants.

Reference solution

Python

def isValid(word: str) -> bool:
    if len(word) < 3:
        return False
    vowel = consonant = False
    for c in word:
        if "a" <= c <= "z" or "A" <= c <= "Z":
            if c in "aeiouAEIOU":
                vowel = True
            else:
                consonant = True
        elif not ("0" <= c <= "9"):
            return False
    return vowel and consonant

Java

public static boolean isValid(String word) {
    if (word.length() < 3) return false;
    boolean vowel = false, consonant = false;
    for (char c : word.toCharArray()) {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            if ("aeiouAEIOU".indexOf(c) >= 0) vowel = true;
            else consonant = true;
        } else if (!(c >= '0' && c <= '9')) {
            return false;
        }
    }
    return vowel && consonant;
}

C++

bool isValid(string word) {
    if (word.size() < 3) return false;
    bool vowel = false, consonant = false;
    for (char c : word) {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            char l = (char) tolower(c);
            if (l == 'a' || l == 'e' || l == 'i' || l == 'o' || l == 'u') vowel = true;
            else consonant = true;
        } else if (!(c >= '0' && c <= '9')) {
            return false;
        }
    }
    return vowel && consonant;
}

JavaScript

var isValid = function(word) {
    if (word.length < 3) return false;
    var vowel = false, consonant = false;
    for (var i = 0; i < word.length; i++) {
        var c = word[i];
        if ((c >= "a" && c <= "z") || (c >= "A" && c <= "Z")) {
            if ("aeiouAEIOU".indexOf(c) >= 0) vowel = true;
            else consonant = true;
        } else if (!(c >= "0" && c <= "9")) {
            return false;
        }
    }
    return vowel && consonant;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings