< Summary

Information
Class: LeetCode.Algorithms.FindTheLengthOfTheLongestCommonPrefix.FindTheLengthOfTheLongestCommonPrefixBruteForce
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLengthOfTheLongestCommonPrefix\FindTheLengthOfTheLongestCommonPrefixBruteForce.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 48
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%11100%
GetCommonPrefixLength(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLengthOfTheLongestCommonPrefix\FindTheLengthOfTheLongestCommonPrefixBruteForce.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 FindTheLengthOfTheLongestCommonPrefixBruteForce : IFindTheLengthOfTheLongestCommonPrefix
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m * d), where n is the length of arr1, m is the length of arr2, d is the average num
 19    ///     digits in the numbers of arr1 and arr2
 20    ///     Space complexity - O(1)
 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        return arr1.Aggregate(0,
 10428            (current, num1) => arr2.Select(num2 => GetCommonPrefixLength(num1, num2)).Prepend(current).Max());
 729    }
 30
 31    private static int GetCommonPrefixLength(int num1, int num2)
 7532    {
 7533        var str1 = num1.ToString();
 7534        var str2 = num2.ToString();
 35
 7536        var minLength = Math.Min(str1.Length, str2.Length);
 37
 25238        for (var i = 0; i < minLength; i++)
 11539        {
 11540            if (str1[i] != str2[i])
 6441            {
 6442                return i;
 43            }
 5144        }
 45
 1146        return minLength;
 7547    }
 48}