| | 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.UniqueLength3PalindromicSubsequences; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public 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) |
| 3 | 28 | | { |
| 3 | 29 | | var firstOccurrence = new int[AlphabetLength]; |
| 3 | 30 | | var lastOccurrence = new int[AlphabetLength]; |
| | 31 | |
|
| 3 | 32 | | Array.Fill(firstOccurrence, -1); |
| 3 | 33 | | Array.Fill(lastOccurrence, -1); |
| | 34 | |
|
| 36 | 35 | | for (var i = 0; i < s.Length; i++) |
| 15 | 36 | | { |
| 15 | 37 | | var index = s[i] - 'a'; |
| | 38 | |
|
| 15 | 39 | | if (firstOccurrence[index] == -1) |
| 9 | 40 | | { |
| 9 | 41 | | firstOccurrence[index] = i; |
| 9 | 42 | | } |
| | 43 | |
|
| 15 | 44 | | lastOccurrence[index] = i; |
| 15 | 45 | | } |
| | 46 | |
|
| 3 | 47 | | var palindromesHashSet = new HashSet<string>(); |
| | 48 | |
|
| 162 | 49 | | for (var i = 0; i < AlphabetLength; i++) |
| 78 | 50 | | { |
| 78 | 51 | | if (firstOccurrence[i] == -1 || firstOccurrence[i] >= lastOccurrence[i]) |
| 75 | 52 | | { |
| 75 | 53 | | continue; |
| | 54 | | } |
| | 55 | |
|
| 3 | 56 | | var start = firstOccurrence[i]; |
| 3 | 57 | | var end = lastOccurrence[i]; |
| | 58 | |
|
| 3 | 59 | | var middleCharsHashSet = new HashSet<char>(); |
| | 60 | |
|
| 22 | 61 | | for (var j = start + 1; j < end; j++) |
| 8 | 62 | | { |
| 8 | 63 | | middleCharsHashSet.Add(s[j]); |
| 8 | 64 | | } |
| | 65 | |
|
| 23 | 66 | | foreach (var middleChar in middleCharsHashSet) |
| 7 | 67 | | { |
| 7 | 68 | | var palindromeStringBuilder = new StringBuilder(); |
| | 69 | |
|
| 7 | 70 | | palindromeStringBuilder.Append(i + 'a'); |
| 7 | 71 | | palindromeStringBuilder.Append(middleChar); |
| 7 | 72 | | palindromeStringBuilder.Append(i + 'a'); |
| | 73 | |
|
| 7 | 74 | | var palindrome = palindromeStringBuilder.ToString(); |
| | 75 | |
|
| 7 | 76 | | palindromesHashSet.Add(palindrome); |
| 7 | 77 | | } |
| 3 | 78 | | } |
| | 79 | |
|
| 3 | 80 | | return palindromesHashSet.Count; |
| 3 | 81 | | } |
| | 82 | | } |