| | 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.UniqueMorseCodeWords; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class UniqueMorseCodeWordsHashSet : IUniqueMorseCodeWords |
| | 18 | | { |
| 2 | 19 | | private readonly Dictionary<char, string> _morseCodeDictionary = new() |
| 2 | 20 | | { |
| 2 | 21 | | { 'a', ".-" }, |
| 2 | 22 | | { 'b', "-..." }, |
| 2 | 23 | | { 'c', "-.-." }, |
| 2 | 24 | | { 'd', "-.." }, |
| 2 | 25 | | { 'e', "." }, |
| 2 | 26 | | { 'f', "..-." }, |
| 2 | 27 | | { 'g', "--." }, |
| 2 | 28 | | { 'h', "...." }, |
| 2 | 29 | | { 'i', ".." }, |
| 2 | 30 | | { 'j', ".---" }, |
| 2 | 31 | | { 'k', "-.-" }, |
| 2 | 32 | | { 'l', ".-.." }, |
| 2 | 33 | | { 'm', "--" }, |
| 2 | 34 | | { 'n', "-." }, |
| 2 | 35 | | { 'o', "---" }, |
| 2 | 36 | | { 'p', ".--." }, |
| 2 | 37 | | { 'q', "--.-" }, |
| 2 | 38 | | { 'r', ".-." }, |
| 2 | 39 | | { 's', "..." }, |
| 2 | 40 | | { 't', "-" }, |
| 2 | 41 | | { 'u', "..-" }, |
| 2 | 42 | | { 'v', "...-" }, |
| 2 | 43 | | { 'w', ".--" }, |
| 2 | 44 | | { 'x', "-..-" }, |
| 2 | 45 | | { 'y', "-.--" }, |
| 2 | 46 | | { 'z', "--.." } |
| 2 | 47 | | }; |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Time complexity - O(n * m) |
| | 51 | | /// Space complexity - O(n * m) |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="words"></param> |
| | 54 | | /// <returns></returns> |
| | 55 | | public int UniqueMorseRepresentations(string[] words) |
| 2 | 56 | | { |
| 2 | 57 | | var morseRepresentationsHashSet = new HashSet<string>(); |
| | 58 | |
|
| 16 | 59 | | foreach (var word in words) |
| 5 | 60 | | { |
| 5 | 61 | | var morseCodeWordStringBuilder = new StringBuilder(); |
| | 62 | |
|
| 54 | 63 | | foreach (var morseCode in word.Select(@char => _morseCodeDictionary[@char])) |
| 13 | 64 | | { |
| 13 | 65 | | morseCodeWordStringBuilder.Append(morseCode); |
| 13 | 66 | | } |
| | 67 | |
|
| 5 | 68 | | morseRepresentationsHashSet.Add(morseCodeWordStringBuilder.ToString()); |
| 5 | 69 | | } |
| | 70 | |
|
| 2 | 71 | | return morseRepresentationsHashSet.Count; |
| 2 | 72 | | } |
| | 73 | | } |