< Summary

Information
Class: LeetCode.Algorithms.FindTheDifference.FindTheDifferenceDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheDifference\FindTheDifferenceDictionary.cs
Line coverage
76%
Covered lines: 13
Uncovered lines: 4
Coverable lines: 17
Total lines: 47
Line coverage: 76.4%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindTheDifference(...)80%111076.47%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheDifference\FindTheDifferenceDictionary.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.FindTheDifference;
 13
 14/// <inheritdoc />
 15public class FindTheDifferenceDictionary : IFindTheDifference
 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 char FindTheDifference(string s, string t)
 325    {
 326        var sDictionary = new Dictionary<char, int>();
 27
 1428        foreach (var sItem in s.Where(sItem => !sDictionary.TryAdd(sItem, 1)))
 029        {
 030            sDictionary[sItem]++;
 031        }
 32
 2233        foreach (var tItem in t)
 834        {
 835            if (sDictionary.TryGetValue(tItem, out var tItemValue) && tItemValue > 0)
 536            {
 537                sDictionary[tItem]--;
 538            }
 39            else
 340            {
 341                return tItem;
 42            }
 543        }
 44
 045        return (char)0;
 346    }
 47}