| | 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.UncommonWordsFromTwoSentences; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class UncommonWordsFromTwoSentencesDictionary : IUncommonWordsFromTwoSentences |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + m) |
| | 19 | | /// Space complexity - O(n + m) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="s1"></param> |
| | 22 | | /// <param name="s2"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public string[] UncommonFromSentences(string s1, string s2) |
| 4 | 25 | | { |
| 4 | 26 | | var wordsDictionary = new Dictionary<string, int>(); |
| | 27 | |
|
| 42 | 28 | | foreach (var word in s1.Split(' ')) |
| 15 | 29 | | { |
| 15 | 30 | | if (!wordsDictionary.TryAdd(word, 1)) |
| 5 | 31 | | { |
| 5 | 32 | | wordsDictionary[word]++; |
| 5 | 33 | | } |
| 15 | 34 | | } |
| | 35 | |
|
| 34 | 36 | | foreach (var word in s2.Split(' ')) |
| 11 | 37 | | { |
| 11 | 38 | | if (!wordsDictionary.TryAdd(word, 1)) |
| 7 | 39 | | { |
| 7 | 40 | | wordsDictionary[word]++; |
| 7 | 41 | | } |
| 11 | 42 | | } |
| | 43 | |
|
| 23 | 44 | | return wordsDictionary.Where(w => w.Value == 1).Select(w => w.Key).ToArray(); |
| 4 | 45 | | } |
| | 46 | | } |