| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.GoatLatin; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class GoatLatinSimulation : IGoatLatin |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="sentence"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public string ToGoatLatin(string sentence) |
| 2 | 26 | | { |
| 2 | 27 | | var resultStringBuilder = new StringBuilder(sentence.Length * 2); |
| 2 | 28 | | var isFirstLetter = true; |
| 2 | 29 | | var firstLetter = '\0'; |
| 2 | 30 | | var wordIndex = 0; |
| | 31 | |
|
| 130 | 32 | | foreach (var character in sentence) |
| 62 | 33 | | { |
| 62 | 34 | | if (isFirstLetter) |
| 13 | 35 | | { |
| 13 | 36 | | firstLetter = character; |
| | 37 | |
|
| 13 | 38 | | isFirstLetter = false; |
| | 39 | |
|
| 13 | 40 | | if (IsVowel(firstLetter)) |
| 2 | 41 | | { |
| 2 | 42 | | resultStringBuilder.Append(firstLetter); |
| 2 | 43 | | } |
| 13 | 44 | | } |
| 49 | 45 | | else if (character == ' ') |
| 11 | 46 | | { |
| 11 | 47 | | CloseWord(resultStringBuilder, firstLetter, ref wordIndex); |
| | 48 | |
|
| 11 | 49 | | resultStringBuilder.Append(' '); |
| | 50 | |
|
| 11 | 51 | | isFirstLetter = true; |
| 11 | 52 | | } |
| | 53 | | else |
| 38 | 54 | | { |
| 38 | 55 | | resultStringBuilder.Append(character); |
| 38 | 56 | | } |
| 62 | 57 | | } |
| | 58 | |
|
| 2 | 59 | | CloseWord(resultStringBuilder, firstLetter, ref wordIndex); |
| | 60 | |
|
| 2 | 61 | | return resultStringBuilder.ToString(); |
| 2 | 62 | | } |
| | 63 | |
|
| | 64 | | private static bool IsVowel(char c) |
| 26 | 65 | | { |
| 26 | 66 | | return char.ToLowerInvariant(c) is 'a' or 'e' or 'i' or 'o' or 'u'; |
| 26 | 67 | | } |
| | 68 | |
|
| | 69 | | private static void CloseWord(StringBuilder stringBuilder, char firstLetter, ref int wordIndex) |
| 13 | 70 | | { |
| 13 | 71 | | if (!IsVowel(firstLetter)) |
| 11 | 72 | | { |
| 11 | 73 | | stringBuilder.Append(firstLetter); |
| 11 | 74 | | } |
| | 75 | |
|
| 13 | 76 | | stringBuilder.Append("ma"); |
| | 77 | |
|
| 13 | 78 | | wordIndex++; |
| | 79 | |
|
| 13 | 80 | | stringBuilder.Append('a', wordIndex); |
| 13 | 81 | | } |
| | 82 | | } |