| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.IsomorphicStrings; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class IsomorphicStringsDictionary : IIsomorphicStrings |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(n) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="s"></param> |
| | | 22 | | /// <param name="t"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public bool IsIsomorphic(string s, string t) |
| | 4 | 25 | | { |
| | 4 | 26 | | var dictionary = new Dictionary<char, char>(); |
| | | 27 | | |
| | 32 | 28 | | for (var i = 0; i < s.Length; i++) |
| | 14 | 29 | | { |
| | 14 | 30 | | if (dictionary.TryGetValue(s[i], out var value)) |
| | 3 | 31 | | { |
| | 3 | 32 | | if (value.Equals(t[i])) |
| | 2 | 33 | | { |
| | 2 | 34 | | continue; |
| | | 35 | | } |
| | | 36 | | |
| | 1 | 37 | | return false; |
| | | 38 | | } |
| | | 39 | | |
| | 11 | 40 | | if (dictionary.ContainsValue(t[i])) |
| | 1 | 41 | | { |
| | 1 | 42 | | return false; |
| | | 43 | | } |
| | | 44 | | |
| | 10 | 45 | | dictionary.Add(s[i], t[i]); |
| | 10 | 46 | | } |
| | | 47 | | |
| | 2 | 48 | | return true; |
| | 4 | 49 | | } |
| | | 50 | | } |