< Summary

Information
Class: LeetCode.Algorithms.FindTheLexicographicallyLargestStringFromTheBox1.FindTheLexicographicallyLargestStringFromTheBox1TwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLexicographicallyLargestStringFromTheBox1\FindTheLexicographicallyLargestStringFromTheBox1TwoPointers.cs
Line coverage
75%
Covered lines: 22
Uncovered lines: 7
Coverable lines: 29
Total lines: 67
Line coverage: 75.8%
Branch coverage
83%
Covered branches: 10
Total branches: 12
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
AnswerString(...)50%2271.42%
GetLastSubstring(...)90%111077.27%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheLexicographicallyLargestStringFromTheBox1\FindTheLexicographicallyLargestStringFromTheBox1TwoPointers.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.FindTheLexicographicallyLargestStringFromTheBox1;
 13
 14/// <inheritdoc />
 15public class FindTheLexicographicallyLargestStringFromTheBox1TwoPointers :
 16    IFindTheLexicographicallyLargestStringFromTheBox1
 17{
 18    /// <summary>
 19    ///     Time complexity - O(n)
 20    ///     Space complexity - O(n)
 21    /// </summary>
 22    /// <param name="word"></param>
 23    /// <param name="numFriends"></param>
 24    /// <returns></returns>
 25    public string AnswerString(string word, int numFriends)
 226    {
 227        if (numFriends == 1)
 028        {
 029            return word;
 30        }
 31
 232        var lastSubstring = GetLastSubstring(word);
 33
 234        return lastSubstring[..Math.Min(lastSubstring.Length, word.Length - numFriends + 1)];
 235    }
 36
 37    private static string GetLastSubstring(string word)
 238    {
 239        var i = 0;
 240        var j = 1;
 41
 642        while (j < word.Length)
 443        {
 444            var k = 0;
 45
 746            while (j + k < word.Length && word[i + k] == word[j + k])
 347            {
 348                k++;
 349            }
 50
 451            if (j + k < word.Length && word[i + k] < word[j + k])
 052            {
 053                var t = i;
 54
 055                i = j;
 56
 057                j = Math.Max(j + 1, t + k + 1);
 058            }
 59            else
 460            {
 461                j = j + k + 1;
 462            }
 463        }
 64
 265        return word[i..];
 266    }
 67}