< Summary

Information
Class: LeetCode.Algorithms.GoatLatin.GoatLatinSimulation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\GoatLatin\GoatLatinSimulation.cs
Line coverage
100%
Covered lines: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ToGoatLatin(...)100%88100%
IsVowel(...)100%1212100%
CloseWord(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\GoatLatin\GoatLatinSimulation.cs

#LineLine coverage
 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
 12using System.Text;
 13
 14namespace LeetCode.Algorithms.GoatLatin;
 15
 16/// <inheritdoc />
 17public 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)
 226    {
 227        var resultStringBuilder = new StringBuilder(sentence.Length * 2);
 228        var isFirstLetter = true;
 229        var firstLetter = '\0';
 230        var wordIndex = 0;
 31
 13032        foreach (var character in sentence)
 6233        {
 6234            if (isFirstLetter)
 1335            {
 1336                firstLetter = character;
 37
 1338                isFirstLetter = false;
 39
 1340                if (IsVowel(firstLetter))
 241                {
 242                    resultStringBuilder.Append(firstLetter);
 243                }
 1344            }
 4945            else if (character == ' ')
 1146            {
 1147                CloseWord(resultStringBuilder, firstLetter, ref wordIndex);
 48
 1149                resultStringBuilder.Append(' ');
 50
 1151                isFirstLetter = true;
 1152            }
 53            else
 3854            {
 3855                resultStringBuilder.Append(character);
 3856            }
 6257        }
 58
 259        CloseWord(resultStringBuilder, firstLetter, ref wordIndex);
 60
 261        return resultStringBuilder.ToString();
 262    }
 63
 64    private static bool IsVowel(char c)
 2665    {
 2666        return char.ToLowerInvariant(c) is 'a' or 'e' or 'i' or 'o' or 'u';
 2667    }
 68
 69    private static void CloseWord(StringBuilder stringBuilder, char firstLetter, ref int wordIndex)
 1370    {
 1371        if (!IsVowel(firstLetter))
 1172        {
 1173            stringBuilder.Append(firstLetter);
 1174        }
 75
 1376        stringBuilder.Append("ma");
 77
 1378        wordIndex++;
 79
 1380        stringBuilder.Append('a', wordIndex);
 1381    }
 82}