Skip to content

Commit bf0407d

Browse files
committed
feat: add solutions to lc problem: No.3136
No.3136.Valid Word
1 parent a666697 commit bf0407d

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

solution/3100-3199/3136.Valid Word/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,71 @@ function isValid(word: string): boolean {
235235
}
236236
```
237237

238+
#### Rust
239+
240+
```rust
241+
impl Solution {
242+
pub fn is_valid(word: String) -> bool {
243+
if word.len() < 3 {
244+
return false;
245+
}
246+
247+
let mut has_vowel = false;
248+
let mut has_consonant = false;
249+
let vowels = ['a', 'e', 'i', 'o', 'u'];
250+
251+
for c in word.chars() {
252+
if !c.is_alphanumeric() {
253+
return false;
254+
}
255+
if c.is_alphabetic() {
256+
let lower_c = c.to_ascii_lowercase();
257+
if vowels.contains(&lower_c) {
258+
has_vowel = true;
259+
} else {
260+
has_consonant = true;
261+
}
262+
}
263+
}
264+
265+
has_vowel && has_consonant
266+
}
267+
}
268+
```
269+
270+
#### C#
271+
272+
```cs
273+
public class Solution {
274+
public bool IsValid(string word) {
275+
if (word.Length < 3) {
276+
return false;
277+
}
278+
279+
bool hasVowel = false, hasConsonant = false;
280+
bool[] vs = new bool[26];
281+
foreach (char c in "aeiou") {
282+
vs[c - 'a'] = true;
283+
}
284+
285+
foreach (char c in word) {
286+
if (char.IsLetter(c)) {
287+
char lower = char.ToLower(c);
288+
if (vs[lower - 'a']) {
289+
hasVowel = true;
290+
} else {
291+
hasConsonant = true;
292+
}
293+
} else if (!char.IsDigit(c)) {
294+
return false;
295+
}
296+
}
297+
298+
return hasVowel && hasConsonant;
299+
}
300+
}
301+
```
302+
238303
<!-- tabs:end -->
239304

240305
<!-- solution:end -->

solution/3100-3199/3136.Valid Word/README_EN.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,71 @@ function isValid(word: string): boolean {
235235
}
236236
```
237237

238+
#### Rust
239+
240+
```rust
241+
impl Solution {
242+
pub fn is_valid(word: String) -> bool {
243+
if word.len() < 3 {
244+
return false;
245+
}
246+
247+
let mut has_vowel = false;
248+
let mut has_consonant = false;
249+
let vowels = ['a', 'e', 'i', 'o', 'u'];
250+
251+
for c in word.chars() {
252+
if !c.is_alphanumeric() {
253+
return false;
254+
}
255+
if c.is_alphabetic() {
256+
let lower_c = c.to_ascii_lowercase();
257+
if vowels.contains(&lower_c) {
258+
has_vowel = true;
259+
} else {
260+
has_consonant = true;
261+
}
262+
}
263+
}
264+
265+
has_vowel && has_consonant
266+
}
267+
}
268+
```
269+
270+
#### C#
271+
272+
```cs
273+
public class Solution {
274+
public bool IsValid(string word) {
275+
if (word.Length < 3) {
276+
return false;
277+
}
278+
279+
bool hasVowel = false, hasConsonant = false;
280+
bool[] vs = new bool[26];
281+
foreach (char c in "aeiou") {
282+
vs[c - 'a'] = true;
283+
}
284+
285+
foreach (char c in word) {
286+
if (char.IsLetter(c)) {
287+
char lower = char.ToLower(c);
288+
if (vs[lower - 'a']) {
289+
hasVowel = true;
290+
} else {
291+
hasConsonant = true;
292+
}
293+
} else if (!char.IsDigit(c)) {
294+
return false;
295+
}
296+
}
297+
298+
return hasVowel && hasConsonant;
299+
}
300+
}
301+
```
302+
238303
<!-- tabs:end -->
239304

240305
<!-- solution:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution {
2+
public bool IsValid(string word) {
3+
if (word.Length < 3) {
4+
return false;
5+
}
6+
7+
bool hasVowel = false, hasConsonant = false;
8+
bool[] vs = new bool[26];
9+
foreach (char c in "aeiou") {
10+
vs[c - 'a'] = true;
11+
}
12+
13+
foreach (char c in word) {
14+
if (char.IsLetter(c)) {
15+
char lower = char.ToLower(c);
16+
if (vs[lower - 'a']) {
17+
hasVowel = true;
18+
} else {
19+
hasConsonant = true;
20+
}
21+
} else if (!char.IsDigit(c)) {
22+
return false;
23+
}
24+
}
25+
26+
return hasVowel && hasConsonant;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn is_valid(word: String) -> bool {
3+
if word.len() < 3 {
4+
return false;
5+
}
6+
7+
let mut has_vowel = false;
8+
let mut has_consonant = false;
9+
let vowels = ['a', 'e', 'i', 'o', 'u'];
10+
11+
for c in word.chars() {
12+
if !c.is_alphanumeric() {
13+
return false;
14+
}
15+
if c.is_alphabetic() {
16+
let lower_c = c.to_ascii_lowercase();
17+
if vowels.contains(&lower_c) {
18+
has_vowel = true;
19+
} else {
20+
has_consonant = true;
21+
}
22+
}
23+
}
24+
25+
has_vowel && has_consonant
26+
}
27+
}

0 commit comments

Comments
 (0)