3136. Valid Word #1930
-
Topics: A word is considered valid if:
You are given a string Return Notes:
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine if a given word meets specific criteria for validity. The criteria are:
Approach
Let's implement this solution in PHP: 3136. Valid Word <?php
/**
* @param String $word
* @return Boolean
*/
function isValid($word) {
$n = strlen($word);
if ($n < 3) {
return false;
}
for ($i = 0; $i < $n; $i++) {
$c = $word[$i];
if (!(($c >= 'a' && $c <= 'z') ||
($c >= 'A' && $c <= 'Z') ||
($c >= '0' && $c <= '9'))) {
return false;
}
}
$vowels = "aeiouAEIOU";
$hasVowel = false;
$hasConsonant = false;
for ($i = 0; $i < $n; $i++) {
$c = $word[$i];
if (($c >= 'a' && $c <= 'z') || ($c >= 'A' && $c <= 'Z')) {
if (strpos($vowels, $c) !== false) {
$hasVowel = true;
} else {
$hasConsonant = true;
}
}
}
return $hasVowel && $hasConsonant;
}
// Test cases
var_dump(isValid("234Adas")); // true
var_dump(isValid("b3")); // false
var_dump(isValid("a3\$e")); // false
?> Explanation:
This approach efficiently verifies all the given conditions step-by-step, ensuring the word meets all criteria for validity. The solution handles edge cases like words with insufficient length, invalid characters, or missing vowels/consonants appropriately. |
Beta Was this translation helpful? Give feedback.
We need to determine if a given word meets specific criteria for validity. The criteria are:
Approach