Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 03e99ce

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.6 MB (98.01%)
1 parent d7b850c commit 03e99ce

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

‎3396-valid-word/README.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<p>A word is considered <strong>valid</strong> if:</p>
2+
3+
<ul>
4+
<li>It contains a <strong>minimum</strong> of 3 characters.</li>
5+
<li>It contains only digits (0-9), and English letters (uppercase and lowercase).</li>
6+
<li>It includes <strong>at least</strong> one <strong>vowel</strong>.</li>
7+
<li>It includes <strong>at least</strong> one <strong>consonant</strong>.</li>
8+
</ul>
9+
10+
<p>You are given a string <code>word</code>.</p>
11+
12+
<p>Return <code>true</code> if <code>word</code> is valid, otherwise, return <code>false</code>.</p>
13+
14+
<p><strong>Notes:</strong></p>
15+
16+
<ul>
17+
<li><code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, <code>&#39;u&#39;</code>, and their uppercases are <strong>vowels</strong>.</li>
18+
<li>A <strong>consonant</strong> is an English letter that is not a vowel.</li>
19+
</ul>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<div class="example-block">
25+
<p><strong>Input:</strong> <span class="example-io">word = &quot;234Adas&quot;</span></p>
26+
27+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
28+
29+
<p><strong>Explanation:</strong></p>
30+
31+
<p>This word satisfies the conditions.</p>
32+
</div>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">word = &quot;b3&quot;</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
40+
41+
<p><strong>Explanation:</strong></p>
42+
43+
<p>The length of this word is fewer than 3, and does not have a vowel.</p>
44+
</div>
45+
46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>Input:</strong> <span class="example-io">word = &quot;a3$e&quot;</span></p>
50+
51+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
52+
53+
<p><strong>Explanation:</strong></p>
54+
55+
<p>This word contains a <code>&#39;$&#39;</code> character and does not have a consonant.</p>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= word.length &lt;= 20</code></li>
63+
<li><code>word</code> consists of English uppercase and lowercase letters, digits, <code>&#39;@&#39;</code>, <code>&#39;#&#39;</code>, and <code>&#39;$&#39;</code>.</li>
64+
</ul>

‎3396-valid-word/solution.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach: One-Time Traversal
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def isValid(self, word: str) -> bool:
8+
if len(word) < 3:
9+
return False
10+
11+
has_vowel = False
12+
has_consonant = False
13+
14+
for c in word:
15+
if c.isalpha():
16+
if c.lower() in 'aeiou':
17+
has_vowel = True
18+
else:
19+
has_consonant = True
20+
elif not c.isdigit():
21+
return False
22+
23+
return has_vowel and has_consonant

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /