< Summary

Information
Class: LeetCode.Algorithms.GenerateTagForVideoCaption.GenerateTagForVideoCaptionCharArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\GenerateTagForVideoCaption\GenerateTagForVideoCaptionCharArray.cs
Line coverage
95%
Covered lines: 38
Uncovered lines: 2
Coverable lines: 40
Total lines: 87
Line coverage: 95%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GenerateTag(...)91.66%121293.75%
TryAppend(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\GenerateTagForVideoCaption\GenerateTagForVideoCaptionCharArray.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
 12namespace LeetCode.Algorithms.GenerateTagForVideoCaption;
 13
 14/// <inheritdoc />
 15public class GenerateTagForVideoCaptionCharArray : IGenerateTagForVideoCaption
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="caption"></param>
 22    /// <returns></returns>
 23    public string GenerateTag(string caption)
 324    {
 325        if (string.IsNullOrWhiteSpace(caption))
 026        {
 027            return "#";
 28        }
 29
 330        var chars = new char[100];
 31
 332        chars[0] = '#';
 33
 334        var isFirstWord = true;
 335        var isWord = false;
 36
 337        var length = 1;
 38
 29639        foreach (var @char in caption)
 14440        {
 14441            if (@char == ' ')
 642            {
 643                isWord = false;
 44
 645                continue;
 46            }
 47
 48            char charToAppend;
 49
 13850            if (isWord)
 12951            {
 12952                charToAppend = char.ToLowerInvariant(@char);
 12953            }
 54            else
 955            {
 956                isWord = true;
 57
 958                charToAppend = isFirstWord
 959                    ? char.ToLowerInvariant(@char)
 960                    : char.ToUpperInvariant(@char);
 61
 962                isFirstWord = false;
 963            }
 64
 13865            if (TryAppend(chars, ref length, charToAppend))
 166            {
 167                break;
 68            }
 13769        }
 70
 371        return new string(chars, 0, length);
 372    }
 73
 74    private static bool TryAppend(char[] chars, ref int length, char @char)
 13875    {
 13876        if (length == 100)
 177        {
 178            return true;
 79        }
 80
 13781        chars[length] = @char;
 82
 13783        length++;
 84
 13785        return false;
 13886    }
 87}