| | 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 UncommonWordsFromTwoSentencesHashSet : 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 once = new HashSet<string>(); |
| 4 | 27 | | var moreThanOnce = new HashSet<string>(); |
| | 28 | |
|
| 42 | 29 | | foreach (var word in s1.Split(' ')) |
| 15 | 30 | | { |
| 15 | 31 | | if (!once.Add(word)) |
| 5 | 32 | | { |
| 5 | 33 | | moreThanOnce.Add(word); |
| 5 | 34 | | } |
| 15 | 35 | | } |
| | 36 | |
|
| 34 | 37 | | foreach (var word in s2.Split(' ')) |
| 11 | 38 | | { |
| 11 | 39 | | if (!once.Add(word)) |
| 7 | 40 | | { |
| 7 | 41 | | moreThanOnce.Add(word); |
| 7 | 42 | | } |
| 11 | 43 | | } |
| | 44 | |
|
| 4 | 45 | | return once.Except(moreThanOnce).ToArray(); |
| 4 | 46 | | } |
| | 47 | | } |