< Summary

Information
Class: LeetCode.Algorithms.PermutationInString.PermutationInStringSlidingWindowArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PermutationInString\PermutationInStringSlidingWindowArray.cs
Line coverage
93%
Covered lines: 28
Uncovered lines: 2
Coverable lines: 30
Total lines: 68
Line coverage: 93.3%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CheckInclusion(...)87.5%8890.47%
AreArraysEqual(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PermutationInString\PermutationInStringSlidingWindowArray.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 PermutationInStringSlidingWindowArray : IPermutationInString
 16{
 17    private const int ArrayLength = 'z' - 'a' + 1;
 18
 19    /// <summary>
 20    ///     Time complexity - O(n + m), where n is the length of s1 and m is the length of s2
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="s1"></param>
 24    /// <param name="s2"></param>
 25    /// <returns></returns>
 26    public bool CheckInclusion(string s1, string s2)
 627    {
 628        if (s1.Length > s2.Length)
 029        {
 030            return false;
 31        }
 32
 633        var s1Array = new int[ArrayLength];
 634        var s2Array = new int[ArrayLength];
 35
 4636        for (var i = 0; i < s1.Length; i++)
 1737        {
 1738            s1Array[s1[i] - 'a']++;
 1739            s2Array[s2[i] - 'a']++;
 1740        }
 41
 5242        for (var i = 0; i < s2.Length - s1.Length; i++)
 2143        {
 2144            if (AreArraysEqual(s1Array, s2Array))
 145            {
 146                return true;
 47            }
 48
 2049            s2Array[s2[i + s1.Length] - 'a']++;
 2050            s2Array[s2[i] - 'a']--;
 2051        }
 52
 553        return AreArraysEqual(s1Array, s2Array);
 654    }
 55
 56    private static bool AreArraysEqual(int[] array1, int[] array2)
 2657    {
 33258        for (var i = 0; i < 26; i++)
 16359        {
 16360            if (array1[i] != array2[i])
 2361            {
 2362                return false;
 63            }
 14064        }
 65
 366        return true;
 2667    }
 68}