| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.ValidWord; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ValidWordIterative : IValidWord |
| | | 16 | | { |
| | 1 | 17 | | private static readonly HashSet<char> Vowels = |
| | 1 | 18 | | [ |
| | 1 | 19 | | 'a', 'e', 'i', 'o', 'u' |
| | 1 | 20 | | ]; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Time complexity - O(n) |
| | | 24 | | /// Space complexity - O(1) |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="word"></param> |
| | | 27 | | /// <returns></returns> |
| | | 28 | | public bool IsValid(string word) |
| | 4 | 29 | | { |
| | 4 | 30 | | if (word.Length < 3) |
| | 1 | 31 | | { |
| | 1 | 32 | | return false; |
| | | 33 | | } |
| | | 34 | | |
| | 3 | 35 | | var hasVowel = false; |
| | 3 | 36 | | var hasConsonant = false; |
| | | 37 | | |
| | 34 | 38 | | foreach (var character in word) |
| | 13 | 39 | | { |
| | 13 | 40 | | if (!char.IsLetterOrDigit(character)) |
| | 1 | 41 | | { |
| | 1 | 42 | | return false; |
| | | 43 | | } |
| | | 44 | | |
| | 12 | 45 | | if (char.IsDigit(character)) |
| | 4 | 46 | | { |
| | 4 | 47 | | continue; |
| | | 48 | | } |
| | | 49 | | |
| | 8 | 50 | | if (Vowels.Contains(char.ToLower(character))) |
| | 5 | 51 | | { |
| | 5 | 52 | | hasVowel = true; |
| | 5 | 53 | | } |
| | | 54 | | else |
| | 3 | 55 | | { |
| | 3 | 56 | | hasConsonant = true; |
| | 3 | 57 | | } |
| | 8 | 58 | | } |
| | | 59 | | |
| | 2 | 60 | | return hasVowel && hasConsonant; |
| | 4 | 61 | | } |
| | | 62 | | } |