| | 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 | | namespace LeetCode.Algorithms.LetterTilePossibilities; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class LetterTilePossibilitiesRecursive : ILetterTilePossibilities |
| | 16 | | { |
| | 17 | | private const int Count = 'Z' - 'A' + 1; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n * n!) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="tiles"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public int NumTilePossibilities(string tiles) |
| 3 | 26 | | { |
| 3 | 27 | | var charCount = new int[Count]; |
| | 28 | |
|
| 29 | 29 | | foreach (var tile in tiles) |
| 10 | 30 | | { |
| 10 | 31 | | charCount[tile - 'A']++; |
| 10 | 32 | | } |
| | 33 | |
|
| 3 | 34 | | return FindSequences(charCount); |
| 3 | 35 | | } |
| | 36 | |
|
| | 37 | | private static int FindSequences(int[] charCount) |
| 200 | 38 | | { |
| 200 | 39 | | var totalCount = 0; |
| | 40 | |
|
| 10800 | 41 | | for (var i = 0; i < Count; i++) |
| 5200 | 42 | | { |
| 5200 | 43 | | if (charCount[i] == 0) |
| 5003 | 44 | | { |
| 5003 | 45 | | continue; |
| | 46 | | } |
| | 47 | |
|
| 197 | 48 | | totalCount++; |
| | 49 | |
|
| 197 | 50 | | charCount[i]--; |
| | 51 | |
|
| 197 | 52 | | totalCount += FindSequences(charCount); |
| | 53 | |
|
| 197 | 54 | | charCount[i]++; |
| 197 | 55 | | } |
| | 56 | |
|
| 200 | 57 | | return totalCount; |
| 200 | 58 | | } |
| | 59 | | } |