Classic problem #3136EasyString
Valid Word
Real-world version: Is the Token Well FormedTracked in this browser only
Read Valid Word on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A token reads token, made up of English letters in either case, digits, and the marks '@', '#' and '$'.
The token is well formed when all of these hold:
- it runs to at least three characters;
- every character is a letter or a digit, so none of the three marks appears anywhere;
- it holds at least one vowel, meaning
'a','e','i','o'or'u'in either case; - it holds at least one consonant, meaning a letter that is not a vowel.
Return whether the token is well formed.
Examples
Example 1
- Input
- token = "aB3"
- Output
- true
Three characters, none of them a mark, with `a` as the vowel and `B` as the consonant. The digit is allowed and counts as neither.
Example 2
- Input
- token = "xyz"
- Output
- false
Long enough and free of marks, but every letter is a consonant, so there is no vowel.
Example 3
- Input
- token = "q@w"
- Output
- false
The mark in the middle is not allowed, whatever else the token holds.
Constraints
- 1 <= token.length <= 20
- Every character of token is an English letter, a digit, or one of '@', '#' and '$'.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def is_valid(token: str) -> bool:Java
public boolean isValid(String token)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 18 test cases: 18 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's