From bf0407d3703fe20988eb8979e376568c5e0a00e5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 15 Jul 2025 06:52:08 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3136 No.3136.Valid Word --- solution/3100-3199/3136.Valid Word/README.md | 65 +++++++++++++++++++ .../3100-3199/3136.Valid Word/README_EN.md | 65 +++++++++++++++++++ .../3100-3199/3136.Valid Word/Solution.cs | 28 ++++++++ .../3100-3199/3136.Valid Word/Solution.rs | 27 ++++++++ 4 files changed, 185 insertions(+) create mode 100644 solution/3100-3199/3136.Valid Word/Solution.cs create mode 100644 solution/3100-3199/3136.Valid Word/Solution.rs diff --git a/solution/3100-3199/3136.Valid Word/README.md b/solution/3100-3199/3136.Valid Word/README.md index 3c6f8d5733a46..ddfa90d944b75 100644 --- a/solution/3100-3199/3136.Valid Word/README.md +++ b/solution/3100-3199/3136.Valid Word/README.md @@ -235,6 +235,71 @@ function isValid(word: string): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn is_valid(word: String) -> bool { + if word.len() < 3 { + return false; + } + + let mut has_vowel = false; + let mut has_consonant = false; + let vowels = ['a', 'e', 'i', 'o', 'u']; + + for c in word.chars() { + if !c.is_alphanumeric() { + return false; + } + if c.is_alphabetic() { + let lower_c = c.to_ascii_lowercase(); + if vowels.contains(&lower_c) { + has_vowel = true; + } else { + has_consonant = true; + } + } + } + + has_vowel && has_consonant + } +} +``` + +#### C# + +```cs +public class Solution { + public bool IsValid(string word) { + if (word.Length < 3) { + return false; + } + + bool hasVowel = false, hasConsonant = false; + bool[] vs = new bool[26]; + foreach (char c in "aeiou") { + vs[c - 'a'] = true; + } + + foreach (char c in word) { + if (char.IsLetter(c)) { + char lower = char.ToLower(c); + if (vs[lower - 'a']) { + hasVowel = true; + } else { + hasConsonant = true; + } + } else if (!char.IsDigit(c)) { + return false; + } + } + + return hasVowel && hasConsonant; + } +} +``` + diff --git a/solution/3100-3199/3136.Valid Word/README_EN.md b/solution/3100-3199/3136.Valid Word/README_EN.md index a9adb8b47100e..246fdca342ac8 100644 --- a/solution/3100-3199/3136.Valid Word/README_EN.md +++ b/solution/3100-3199/3136.Valid Word/README_EN.md @@ -235,6 +235,71 @@ function isValid(word: string): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn is_valid(word: String) -> bool { + if word.len() < 3 { + return false; + } + + let mut has_vowel = false; + let mut has_consonant = false; + let vowels = ['a', 'e', 'i', 'o', 'u']; + + for c in word.chars() { + if !c.is_alphanumeric() { + return false; + } + if c.is_alphabetic() { + let lower_c = c.to_ascii_lowercase(); + if vowels.contains(&lower_c) { + has_vowel = true; + } else { + has_consonant = true; + } + } + } + + has_vowel && has_consonant + } +} +``` + +#### C# + +```cs +public class Solution { + public bool IsValid(string word) { + if (word.Length < 3) { + return false; + } + + bool hasVowel = false, hasConsonant = false; + bool[] vs = new bool[26]; + foreach (char c in "aeiou") { + vs[c - 'a'] = true; + } + + foreach (char c in word) { + if (char.IsLetter(c)) { + char lower = char.ToLower(c); + if (vs[lower - 'a']) { + hasVowel = true; + } else { + hasConsonant = true; + } + } else if (!char.IsDigit(c)) { + return false; + } + } + + return hasVowel && hasConsonant; + } +} +``` + diff --git a/solution/3100-3199/3136.Valid Word/Solution.cs b/solution/3100-3199/3136.Valid Word/Solution.cs new file mode 100644 index 0000000000000..b60405239a670 --- /dev/null +++ b/solution/3100-3199/3136.Valid Word/Solution.cs @@ -0,0 +1,28 @@ +public class Solution { + public bool IsValid(string word) { + if (word.Length < 3) { + return false; + } + + bool hasVowel = false, hasConsonant = false; + bool[] vs = new bool[26]; + foreach (char c in "aeiou") { + vs[c - 'a'] = true; + } + + foreach (char c in word) { + if (char.IsLetter(c)) { + char lower = char.ToLower(c); + if (vs[lower - 'a']) { + hasVowel = true; + } else { + hasConsonant = true; + } + } else if (!char.IsDigit(c)) { + return false; + } + } + + return hasVowel && hasConsonant; + } +} diff --git a/solution/3100-3199/3136.Valid Word/Solution.rs b/solution/3100-3199/3136.Valid Word/Solution.rs new file mode 100644 index 0000000000000..c5aa40d391aff --- /dev/null +++ b/solution/3100-3199/3136.Valid Word/Solution.rs @@ -0,0 +1,27 @@ +impl Solution { + pub fn is_valid(word: String) -> bool { + if word.len() < 3 { + return false; + } + + let mut has_vowel = false; + let mut has_consonant = false; + let vowels = ['a', 'e', 'i', 'o', 'u']; + + for c in word.chars() { + if !c.is_alphanumeric() { + return false; + } + if c.is_alphabetic() { + let lower_c = c.to_ascii_lowercase(); + if vowels.contains(&lower_c) { + has_vowel = true; + } else { + has_consonant = true; + } + } + } + + has_vowel && has_consonant + } +}