< Summary

Information
Class: LeetCode.Algorithms.FindTheLengthOfTheLongestCommonPrefix.FindTheLengthOfTheLongestCommonPrefixHashSet
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLengthOfTheLongestCommonPrefix\FindTheLengthOfTheLongestCommonPrefixHashSet.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 61
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LongestCommonPrefix(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLengthOfTheLongestCommonPrefix\FindTheLengthOfTheLongestCommonPrefixHashSet.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 FindTheLengthOfTheLongestCommonPrefixHashSet : IFindTheLengthOfTheLongestCommonPrefix
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * d1 + n * d2), where m is the number of elements in arr1, n is the number of elements
 19    ///     arr2, d1 is the average number of digits in arr1, and d2 is the average number of digits in arr2
 20    ///     Space complexity - O(m * d1), where m is the number of elements in arr1, d1 is the average number of digits 
 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 arr1PrefixesHashSet = new HashSet<int>();
 28
 5829        for (var i = 0; i < arr1.Length; i++)
 2230        {
 12531            while (arr1[i] > 0)
 10332            {
 10333                arr1PrefixesHashSet.Add(arr1[i]);
 34
 10335                arr1[i] /= 10;
 10336            }
 2237        }
 38
 739        var longestPrefix = 0;
 40
 5641        for (var i = 0; i < arr2.Length; i++)
 2142        {
 7243            while (arr2[i] > 0)
 6444            {
 6445                if (arr1PrefixesHashSet.Contains(arr2[i]))
 1346                {
 1347                    break;
 48                }
 49
 5150                arr2[i] /= 10;
 5151            }
 52
 2153            if (arr2[i] > 0)
 1354            {
 1355                longestPrefix = Math.Max(longestPrefix, (int)Math.Floor(Math.Log10(arr2[i])) + 1);
 1356            }
 2157        }
 58
 759        return longestPrefix;
 760    }
 61}