< Summary

Information
Class: LeetCode.Algorithms.ValidAnagram.ValidAnagramDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidAnagram\ValidAnagramDictionary.cs
Line coverage
90%
Covered lines: 19
Uncovered lines: 2
Coverable lines: 21
Total lines: 55
Line coverage: 90.4%
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
IsAnagram(...)91.66%121290.47%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidAnagram\ValidAnagramDictionary.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.ValidAnagram;
 13
 14/// <inheritdoc />
 15public class ValidAnagramDictionary : IValidAnagram
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + m)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <param name="t"></param>
 23    /// <returns></returns>
 24    public bool IsAnagram(string s, string t)
 325    {
 326        if (s.Length != t.Length)
 127        {
 128            return false;
 29        }
 30
 231        var dictionary = new Dictionary<char, int>();
 32
 2033        foreach (var c in s.Where(c => !dictionary.TryAdd(c, 1)))
 234        {
 235            dictionary[c]++;
 236        }
 37
 2138        foreach (var c in t)
 839        {
 840            if (!dictionary.TryGetValue(c, out var value))
 141            {
 142                return false;
 43            }
 44
 745            dictionary[c] = --value;
 46
 747            if (value < 0)
 048            {
 049                return false;
 50            }
 751        }
 52
 153        return true;
 354    }
 55}