< Summary

Information
Class: LeetCode.Algorithms.PermutationInString.PermutationInStringSlidingWindowDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PermutationInString\PermutationInStringSlidingWindowDictionary.cs
Line coverage
95%
Covered lines: 38
Uncovered lines: 2
Coverable lines: 40
Total lines: 78
Line coverage: 95%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CheckInclusion(...)92.85%141493.33%
AreDictionariesEqual(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PermutationInString\PermutationInStringSlidingWindowDictionary.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.PermutationInString;
 13
 14/// <inheritdoc />
 15public 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)
 625    {
 626        if (s2.Length < s1.Length)
 027        {
 028            return false;
 29        }
 30
 631        var s1Dictionary = new Dictionary<char, int>();
 632        var s2Dictionary = new Dictionary<char, int>();
 33
 4634        for (var i = 0; i < s1.Length; i++)
 1735        {
 1736            if (!s1Dictionary.TryAdd(s1[i], 1))
 137            {
 138                s1Dictionary[s1[i]]++;
 139            }
 40
 1741            if (!s2Dictionary.TryAdd(s2[i], 1))
 442            {
 443                s2Dictionary[s2[i]]++;
 444            }
 1745        }
 46
 5247        for (var i = 0; i < s2.Length - s1.Length; i++)
 2148        {
 2149            if (AreDictionariesEqual(s1Dictionary, s2Dictionary))
 150            {
 151                return true;
 52            }
 53
 2054            if (!s2Dictionary.TryAdd(s2[i + s1.Length], 1))
 755            {
 756                s2Dictionary[s2[i + s1.Length]]++;
 757            }
 58
 2059            s2Dictionary[s2[i]]--;
 2060        }
 61
 562        return AreDictionariesEqual(s1Dictionary, s2Dictionary);
 663    }
 64
 65    private static bool AreDictionariesEqual(Dictionary<char, int> dictionary1, Dictionary<char, int> dictionary2)
 2666    {
 13167        foreach (var dictionary1Item in dictionary1)
 3868        {
 3869            if (!dictionary2.TryGetValue(dictionary1Item.Key, out var dictionary2ItemValue) ||
 3870                dictionary2ItemValue != dictionary1Item.Value)
 2371            {
 2372                return false;
 73            }
 1574        }
 75
 376        return true;
 2677    }
 78}