3136. Valid Word
EasyView on LeetCode
Time: O(n)
Space: O(1)
Advertisement
Intuition
Check if number is valid: no leading zeros (if multi-digit) and contains only digits and at most one decimal point.
Algorithm
- 1Validate: all chars are digits or one decimal point. No leading zeros unless single digit or decimal.
Common Pitfalls
- •Edge cases: "0", "0.1", ".5" (invalid leading decimal in some definitions). Read problem carefully.
3136.cs
C#
// Approach: Check length ≥ 3, all alnum, has ≥ 1 vowel and ≥ 1 consonant.
// Time: O(n) Space: O(1)
public class Solution
{
public bool IsValid(string word)
{
return word.Length >= 3 && word.All(char.IsLetterOrDigit) &&
word.Any(c => IsVowel(c)) &&
word.Any(c => IsConsonant(c));
}
private bool IsVowel(char c)
{
return "aeiouAEIOU".IndexOf(c) != -1;
}
private bool IsConsonant(char c)
{
return char.IsLetter(c) && !IsVowel(c);
}
}Advertisement
Was this solution helpful?