< Summary

Information
Class: LeetCode.Algorithms.LongestCommonPrefix.LongestCommonPrefixDivideAndConquer
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestCommonPrefix\LongestCommonPrefixDivideAndConquer.cs
Line coverage
96%
Covered lines: 25
Uncovered lines: 1
Coverable lines: 26
Total lines: 65
Line coverage: 96.1%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LongestCommonPrefix(...)83.33%6691.66%
GetLongestPrefix(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestCommonPrefix\LongestCommonPrefixDivideAndConquer.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.LongestCommonPrefix;
 13
 14/// <inheritdoc />
 15public class LongestCommonPrefixDivideAndConquer : ILongestCommonPrefix
 16{
 17    /// <summary>
 18    ///     Time complexity - O(S), where S is the sum of all characters in all strings
 19    ///     Space complexity - O(S), where S is the sum of all characters in all strings
 20    /// </summary>
 21    /// <param name="strs"></param>
 22    /// <returns></returns>
 23    public string LongestCommonPrefix(string[] strs)
 3224    {
 3225        switch (strs.Length)
 26        {
 27            case > 2:
 928                {
 929                    var partLength = strs.Length / 2;
 930                    var leftPart = strs.Take(partLength).ToArray();
 931                    var rightPart = strs.Skip(partLength).Take(strs.Length - partLength).ToArray();
 32
 933                    return GetLongestPrefix(LongestCommonPrefix(leftPart), LongestCommonPrefix(rightPart));
 34                }
 35            case 2:
 1536                {
 1537                    return GetLongestPrefix(strs[0], strs[1]);
 38                }
 39            case 1:
 840                return strs[0];
 41        }
 42
 043        return string.Empty;
 3244    }
 45
 46    private static string GetLongestPrefix(string str1, string str2)
 2447    {
 2448        var minLength = Math.Min(str1.Length, str2.Length);
 2449        var prefixLength = 0;
 50
 23051        for (var i = 0; i < minLength; i++)
 10152        {
 10153            if (str1[i] == str2[i])
 9154            {
 9155                prefixLength++;
 9156            }
 57            else
 1058            {
 1059                break;
 60            }
 9161        }
 62
 2463        return str1[..prefixLength];
 2464    }
 65}