< Summary

Information
Class: LeetCode.Algorithms.LongestPalindromeByConcatenatingTwoLetterWords.LongestPalindromeByConcatenatingTwoLetterWordsFrequencyArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestPalindromeByConcatenatingTwoLetterWords\LongestPalindromeByConcatenatingTwoLetterWordsFrequencyArray.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 59
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LongestPalindrome(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestPalindromeByConcatenatingTwoLetterWords\LongestPalindromeByConcatenatingTwoLetterWordsFrequencyArray.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.LongestPalindromeByConcatenatingTwoLetterWords;
 13
 14/// <inheritdoc />
 15public class LongestPalindromeByConcatenatingTwoLetterWordsFrequencyArray :
 16    ILongestPalindromeByConcatenatingTwoLetterWords
 17{
 18    private const int Length = 'z' - 'a' + 1;
 19
 20    /// <summary>
 21    ///     Time complexity - O(n)
 22    ///     Space complexity - O(1)
 23    /// </summary>
 24    /// <param name="words"></param>
 25    /// <returns></returns>
 26    public int LongestPalindrome(string[] words)
 327    {
 328        var longestPalindrome = 0;
 29
 330        var frequencyArray = new int[Length, Length];
 31
 3332        foreach (var word in words)
 1233        {
 1234            var a = word[0] - 'a';
 1235            var b = word[1] - 'a';
 36
 1237            if (frequencyArray[b, a] > 0)
 338            {
 339                longestPalindrome += 4;
 40
 341                frequencyArray[b, a]--;
 342            }
 43            else
 944            {
 945                frequencyArray[a, b]++;
 946            }
 1247        }
 48
 7449        for (var i = 0; i < Length; i++)
 3650        {
 3651            if (frequencyArray[i, i] > 0)
 252            {
 253                return longestPalindrome + 2;
 54            }
 3455        }
 56
 157        return longestPalindrome;
 358    }
 59}