< Summary

Information
Class: LeetCode.Algorithms.WordPattern.WordPatternTwoDictionaries
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\WordPattern\WordPatternTwoDictionaries.cs
Line coverage
96%
Covered lines: 27
Uncovered lines: 1
Coverable lines: 28
Total lines: 63
Line coverage: 96.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
WordPattern(...)91.66%121296.42%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\WordPattern\WordPatternTwoDictionaries.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.WordPattern;
 13
 14/// <inheritdoc />
 15public class WordPatternTwoDictionaries : IWordPattern
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + m), where n is the length of s and m is the length of pattern
 19    ///     Space complexity - O(m + k), where m is the number of unique characters in pattern and k is the number of un
 20    ///     words in s
 21    /// </summary>
 22    /// <param name="pattern"></param>
 23    /// <param name="s"></param>
 24    /// <returns></returns>
 25    public bool WordPattern(string pattern, string s)
 526    {
 527        var patternHashSet = new Dictionary<char, string>();
 528        var sHashSet = new Dictionary<string, char>();
 29
 530        var sArray = s.Split(' ');
 31
 532        if (pattern.Length != sArray.Length)
 133        {
 134            return false;
 35        }
 36
 2637        for (var i = 0; i < pattern.Length; i++)
 1238        {
 1239            if (patternHashSet.TryGetValue(pattern[i], out var patternValue))
 540            {
 541                if (patternValue != sArray[i])
 242                {
 243                    return false;
 44                }
 345            }
 46            else
 747            {
 748                if (sHashSet.TryGetValue(sArray[i], out var sValue))
 149                {
 150                    if (sValue != pattern[i])
 151                    {
 152                        return false;
 53                    }
 054                }
 55
 656                sHashSet.Add(sArray[i], pattern[i]);
 657                patternHashSet.Add(pattern[i], sArray[i]);
 658            }
 959        }
 60
 161        return true;
 562    }
 63}