Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- public class WordFlowDetector
- {
- private HashSet<char> vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'y' };
- public bool IsWellFlowed(string word)
- {
- List<string> syllables = SplitIntoSyllables(word);
- bool isVowelStart = false;
- foreach (string syllable in syllables)
- {
- bool isSyllableWellFlowed = IsSyllableWellFlowed(syllable, isVowelStart);
- if (!isSyllableWellFlowed)
- return false;
- isVowelStart = !isVowelStart;
- }
- return true;
- }
- private List<string> SplitIntoSyllables(string word)
- {
- List<string> syllables = new List<string>();
- StringBuilder currentSyllable = new StringBuilder();
- for (int i = 0; i < word.Length; i++)
- {
- char currentChar = word[i];
- currentSyllable.Append(currentChar);
- if (i == word.Length - 1 || IsVowel(currentChar) != IsVowel(word[i + 1])))
- {
- syllables.Add(currentSyllable.ToString());
- currentSyllable.Clear();
- }
- }
- return syllables;
- }
- private bool IsSyllableWellFlowed(string syllable, bool isVowelStart)
- {
- bool isVowelExpected = IsVowel(syllable[0]) == isVowelStart;
- for (int i = 0; i < syllable.Length; i++)
- {
- if (IsVowel(syllable[i]) != isVowelExpected)
- return false;
- }
- return true;
- }
- private bool IsVowel(char c)
- {
- return vowels.Contains(c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement