< Summary

Information
Class: LeetCode.Algorithms.CountPrefixAndSuffixPairs1.CountPrefixAndSuffixPairs1Trie
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountPrefixAndSuffixPairs1\CountPrefixAndSuffixPairs1Trie.cs
Line coverage
97%
Covered lines: 65
Uncovered lines: 2
Coverable lines: 67
Total lines: 118
Line coverage: 97%
Branch coverage
91%
Covered branches: 22
Total branches: 24
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
.ctor()100%11100%
CountPrefixSuffixPairs(...)100%88100%
IsPrefixAndSuffix(...)100%22100%
IsPrefix(...)50%22100%
IsSuffix(...)75%4480%
InsertPrefix(...)100%44100%
InsertSuffix(...)100%44100%
get_Nodes()100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountPrefixAndSuffixPairs1\CountPrefixAndSuffixPairs1Trie.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.CountPrefixAndSuffixPairs1;
 13
 14/// <inheritdoc />
 15public class CountPrefixAndSuffixPairs1Trie : ICountPrefixAndSuffixPairs1
 16{
 317    private readonly Node _prefixRoot = new();
 318    private readonly Node _suffixRoot = new();
 19
 20    /// <summary>
 21    ///     Time complexity - O(n^2 * L), where L is the average word length
 22    ///     Space complexity - O(n * L), where L is the average word length
 23    /// </summary>
 24    /// <param name="words"></param>
 25    /// <returns></returns>
 26    public int CountPrefixSuffixPairs(string[] words)
 327    {
 2928        foreach (var word in words)
 1029        {
 1030            InsertPrefix(word);
 1031            InsertSuffix(word);
 1032        }
 33
 334        var count = 0;
 35
 2636        for (var i = 0; i < words.Length; i++)
 1037        {
 4638            for (var j = i + 1; j < words.Length; j++)
 1339            {
 1340                if (IsPrefixAndSuffix(words[i], words[j]))
 641                {
 642                    count++;
 643                }
 1344            }
 1045        }
 46
 347        return count;
 348    }
 49
 50    private bool IsPrefixAndSuffix(string prefixSuffix, string word)
 1351    {
 1352        return IsPrefix(prefixSuffix, word) && IsSuffix(prefixSuffix, word);
 1353    }
 54
 55    private bool IsPrefix(string prefix, string word)
 1356    {
 1357        var currentNode = _prefixRoot;
 58
 5459        return word.All(c => currentNode.Nodes.TryGetValue(c, out currentNode)) && word.StartsWith(prefix);
 1360    }
 61
 62    private bool IsSuffix(string suffix, string word)
 663    {
 664        var currentNode = _suffixRoot;
 5865        for (var i = word.Length - 1; i >= 0; i--)
 2366        {
 2367            if (!currentNode.Nodes.TryGetValue(word[i], out currentNode))
 068            {
 069                return false;
 70            }
 2371        }
 72
 673        return word.EndsWith(suffix);
 674    }
 75
 76    private void InsertPrefix(string word)
 1077    {
 1078        var currentNode = _prefixRoot;
 79
 8880        foreach (var c in word)
 2981        {
 2982            if (currentNode.Nodes.TryGetValue(c, out var node))
 1183            {
 1184                currentNode = node;
 1185            }
 86            else
 1887            {
 1888                currentNode.Nodes[c] = new Node();
 89
 1890                currentNode = currentNode.Nodes[c];
 1891            }
 2992        }
 1093    }
 94
 95    private void InsertSuffix(string word)
 1096    {
 1097        var currentNode = _suffixRoot;
 98
 7899        for (var i = word.Length - 1; i >= 0; i--)
 29100        {
 29101            if (currentNode.Nodes.TryGetValue(word[i], out var node))
 12102            {
 12103                currentNode = node;
 12104            }
 105            else
 17106            {
 17107                currentNode.Nodes[word[i]] = new Node();
 108
 17109                currentNode = currentNode.Nodes[word[i]];
 17110            }
 29111        }
 10112    }
 113
 114    private class Node
 115    {
 233116        public Dictionary<char, Node> Nodes { get; } = new();
 117    }
 118}