< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountPalindromicSubsequence(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\UniqueLength3PalindromicSubsequences\UniqueLength3PalindromicSubsequencesCounting.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.UniqueLength3PalindromicSubsequences;
 15
 16/// <inheritdoc />
 17public class UniqueLength3PalindromicSubsequencesCounting : IUniqueLength3PalindromicSubsequences
 18{
 19    private const int AlphabetLength = 'z' - 'a' + 1;
 20
 21    /// <summary>
 22    ///     Time complexity - O(n * 26)
 23    ///     Space complexity - O(n)
 24    /// </summary>
 25    /// <param name="s"></param>
 26    /// <returns></returns>
 27    public int CountPalindromicSubsequence(string s)
 328    {
 329        var firstOccurrence = new int[AlphabetLength];
 330        var lastOccurrence = new int[AlphabetLength];
 31
 332        Array.Fill(firstOccurrence, -1);
 333        Array.Fill(lastOccurrence, -1);
 34
 3635        for (var i = 0; i < s.Length; i++)
 1536        {
 1537            var index = s[i] - 'a';
 38
 1539            if (firstOccurrence[index] == -1)
 940            {
 941                firstOccurrence[index] = i;
 942            }
 43
 1544            lastOccurrence[index] = i;
 1545        }
 46
 347        var palindromesHashSet = new HashSet<string>();
 48
 16249        for (var i = 0; i < AlphabetLength; i++)
 7850        {
 7851            if (firstOccurrence[i] == -1 || firstOccurrence[i] >= lastOccurrence[i])
 7552            {
 7553                continue;
 54            }
 55
 356            var start = firstOccurrence[i];
 357            var end = lastOccurrence[i];
 58
 359            var middleCharsHashSet = new HashSet<char>();
 60
 2261            for (var j = start + 1; j < end; j++)
 862            {
 863                middleCharsHashSet.Add(s[j]);
 864            }
 65
 2366            foreach (var middleChar in middleCharsHashSet)
 767            {
 768                var palindromeStringBuilder = new StringBuilder();
 69
 770                palindromeStringBuilder.Append(i + 'a');
 771                palindromeStringBuilder.Append(middleChar);
 772                palindromeStringBuilder.Append(i + 'a');
 73
 774                var palindrome = palindromeStringBuilder.ToString();
 75
 776                palindromesHashSet.Add(palindrome);
 777            }
 378        }
 79
 380        return palindromesHashSet.Count;
 381    }
 82}