| | 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.PermutationInString; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class PermutationInStringSlidingWindowDictionary : IPermutationInString |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + m), where n is the length of s1 and m is the length of s2 |
| | 19 | | /// Space complexity - O(k), where k is the number of unique characters in the input |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="s1"></param> |
| | 22 | | /// <param name="s2"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public bool CheckInclusion(string s1, string s2) |
| 6 | 25 | | { |
| 6 | 26 | | if (s2.Length < s1.Length) |
| 0 | 27 | | { |
| 0 | 28 | | return false; |
| | 29 | | } |
| | 30 | |
|
| 6 | 31 | | var s1Dictionary = new Dictionary<char, int>(); |
| 6 | 32 | | var s2Dictionary = new Dictionary<char, int>(); |
| | 33 | |
|
| 46 | 34 | | for (var i = 0; i < s1.Length; i++) |
| 17 | 35 | | { |
| 17 | 36 | | if (!s1Dictionary.TryAdd(s1[i], 1)) |
| 1 | 37 | | { |
| 1 | 38 | | s1Dictionary[s1[i]]++; |
| 1 | 39 | | } |
| | 40 | |
|
| 17 | 41 | | if (!s2Dictionary.TryAdd(s2[i], 1)) |
| 4 | 42 | | { |
| 4 | 43 | | s2Dictionary[s2[i]]++; |
| 4 | 44 | | } |
| 17 | 45 | | } |
| | 46 | |
|
| 52 | 47 | | for (var i = 0; i < s2.Length - s1.Length; i++) |
| 21 | 48 | | { |
| 21 | 49 | | if (AreDictionariesEqual(s1Dictionary, s2Dictionary)) |
| 1 | 50 | | { |
| 1 | 51 | | return true; |
| | 52 | | } |
| | 53 | |
|
| 20 | 54 | | if (!s2Dictionary.TryAdd(s2[i + s1.Length], 1)) |
| 7 | 55 | | { |
| 7 | 56 | | s2Dictionary[s2[i + s1.Length]]++; |
| 7 | 57 | | } |
| | 58 | |
|
| 20 | 59 | | s2Dictionary[s2[i]]--; |
| 20 | 60 | | } |
| | 61 | |
|
| 5 | 62 | | return AreDictionariesEqual(s1Dictionary, s2Dictionary); |
| 6 | 63 | | } |
| | 64 | |
|
| | 65 | | private static bool AreDictionariesEqual(Dictionary<char, int> dictionary1, Dictionary<char, int> dictionary2) |
| 26 | 66 | | { |
| 131 | 67 | | foreach (var dictionary1Item in dictionary1) |
| 38 | 68 | | { |
| 38 | 69 | | if (!dictionary2.TryGetValue(dictionary1Item.Key, out var dictionary2ItemValue) || |
| 38 | 70 | | dictionary2ItemValue != dictionary1Item.Value) |
| 23 | 71 | | { |
| 23 | 72 | | return false; |
| | 73 | | } |
| 15 | 74 | | } |
| | 75 | |
|
| 3 | 76 | | return true; |
| 26 | 77 | | } |
| | 78 | | } |