File tree Expand file tree Collapse file tree 1 file changed +10
-1
lines changed
pystrings/lexicographically_largest_string Expand file tree Collapse file tree 1 file changed +10
-1
lines changed Original file line number Diff line number Diff line change @@ -39,24 +39,33 @@ def lexicographically_largest_string_from_box(word: str, num: int) -> str:
3939
4040 return max_substring
4141
42- # Function to find the lexicographically largest string
42+
4343def lexicographically_largest_string_from_box_2 (word : str , num : int ) -> str :
44+ # If there's only one friend, no split is needed; return the entire string
4445 if num == 1 :
4546 return word
4647
4748 n = len (word )
49+ # i: start index of current best substring, j: candidate start index
4850 i , j = 0 , 1
4951
5052 while j < n :
5153 k = 0
54+ # Compare characters at i+k and j+k as long as they're equal and in bounds
5255 while j + k < n and word [i + k ] == word [j + k ]:
5356 k += 1
5457
58+ # If the candidate substring starting at j is better, update i
5559 if j + k < n and word [i + k ] < word [j + k ]:
60+ # save current i to compute safe next j
5661 temp_index = i
62+ # move i to a better candidate
5763 i = j
64+ # skip redundant comparisons
5865 j = max (j + 1 , temp_index + k + 1 )
5966 else :
67+ # Otherwise, continue searching by skipping the compared section
6068 j = j + k + 1
6169
70+ # Return the best substring of required length: len(word) - numFriends + 1
6271 return word [i : min (n , i + n - num + 1 )]
You can’t perform that action at this time.
0 commit comments