< Summary

Information
Class: LeetCode.Algorithms.FindTheLengthOfTheLongestCommonPrefix.FindTheLengthOfTheLongestCommonPrefixTrie
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLengthOfTheLongestCommonPrefix\FindTheLengthOfTheLongestCommonPrefixTrie.cs
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 96
Line coverage: 100%
Branch coverage
83%
Covered branches: 15
Total branches: 18
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LongestCommonPrefix(...)100%11100%
get_Children()100%11100%
.ctor(...)100%11100%
AddRange(...)100%22100%
Add(...)80%1010100%
FindLongestPrefix(...)83.33%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLengthOfTheLongestCommonPrefix\FindTheLengthOfTheLongestCommonPrefixTrie.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.FindTheLengthOfTheLongestCommonPrefix;
 13
 14/// <inheritdoc />
 15public class FindTheLengthOfTheLongestCommonPrefixTrie : IFindTheLengthOfTheLongestCommonPrefix
 16{
 17    /// <summary>
 18    ///     Time complexity - O((n + m) * d), where n is the number of elements in arr1, m is the number of elements in
 19    ///     arr2, d is the average number of digits in the numbers
 20    ///     Space complexity - O(n * d), where n is the number of elements in arr1, d is the average number of digits in
 21    /// </summary>
 22    /// <param name="arr1"></param>
 23    /// <param name="arr2"></param>
 24    /// <returns></returns>
 25    public int LongestCommonPrefix(int[] arr1, int[] arr2)
 726    {
 727        var trie = new Trie(arr1);
 28
 729        return arr2.Select(trie.FindLongestPrefix).Prepend(0).Max();
 730    }
 31
 32    private class TrieNode
 33    {
 51834        public TrieNode?[] Children { get; } = new TrieNode[10];
 35    }
 36
 37    private class Trie
 38    {
 739        private readonly TrieNode _root = new();
 40
 741        public Trie(IEnumerable<int> nums)
 742        {
 743            AddRange(nums);
 744        }
 45
 46        private void AddRange(IEnumerable<int> nums)
 747        {
 6548            foreach (var num in nums)
 2249            {
 2250                Add(num);
 2251            }
 752        }
 53
 54        private void Add(int num)
 2255        {
 2256            var node = _root;
 2257            var numStr = num.ToString();
 58
 37559            foreach (var idx in numStr.Select(digit => digit - '0'))
 10360            {
 10361                if (node?.Children[idx] == null)
 9962                {
 9963                    if (node != null)
 9964                    {
 9965                        node.Children[idx] = new TrieNode();
 9966                    }
 9967                }
 68
 10369                node = node?.Children[idx];
 10370            }
 2271        }
 72
 73        public int FindLongestPrefix(int num)
 2174        {
 2175            var node = _root;
 2176            var numStr = num.ToString();
 2177            var longestPrefix = 0;
 78
 23179            foreach (var idx in numStr.Select(digit => digit - '0'))
 6180            {
 6181                if (node?.Children[idx] != null)
 4682                {
 4683                    longestPrefix++;
 84
 4685                    node = node.Children[idx];
 4686                }
 87                else
 1588                {
 1589                    break;
 90                }
 4691            }
 92
 2193            return longestPrefix;
 2194        }
 95    }
 96}