The trick here is to name the state correctly, then let the implementation follow. For 1163. Last Substring in Lexicographical Order, the solution in this repository is mainly a greedy solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
The notes already sitting in the source point us in the right direction:
- TLE
- 22 / 24 test cases passed.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are lastSubstring.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//22 / 24 test cases passed.
03class Solution {
04public:
05 string lastSubstring(string s) {
06 int maxIdx = 0;
07 char maxC = '\0';
08 for(int i = 0; i < s.size(); i++){
09 if(s[i] > maxC){
10 maxIdx = i;
11 maxC = s[i];
12 }
13 }
14
15 vector<string> candidates;
16 for(int i = 0; i < s.size(); i++){
17 if(s[i] == maxC){
18 candidates.push_back(s.substr(i));
19 }
20 }
21
22 // cout << candidates.size() << endl;
23
24 // sort(candidates.begin(), candidates.end(),
25 // [](const string& a, const string& b){
26 // return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
27 // });
28
29 nth_element(candidates.begin(), candidates.begin()+candidates.size()-1, candidates.end(),
30 [](const string& a, const string& b){
31 return lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
32 });
33
34 return candidates[candidates.size()-1];
35 }
36};
37
38//https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/363662/Short-python-code-O(n)-time-and-O(1)-space-with-proof-and-visualization
39//Runtime: 104 ms, faster than 77.02% of C++ online submissions for Last Substring in Lexicographical Order.
40//Memory Usage: 14.4 MB, less than 34.68% of C++ online submissions for Last Substring in Lexicographical Order.
41class Solution {
42public:
43 string lastSubstring(string s) {
44 int i = 0; //the start index of final substr
45 int j = i+1; //the start index of the substr for comparison
46 int k = 0; //k = the length of final substr - 1
47 int n = s.size();
48
49 while(j + k < n){
50 // cout << s.substr(i, k+1) << " " << s.substr(j, k+1) << endl;
51 if(s[i+k] == s[j+k]){
52 ++k;
53 }else if(s[i+k] < s[j+k]){
54 /*
55 all char in s[i+1...j-1] are smaller than s[j],
56 so we can safely skip them
57 */
58 i = j;
59 ++j;
60 k = 0;
61 }else{
62 //s[i+k] > s[j+k]
63 /*
64 because s[j...j+k] must be <= s[i],
65 (otherwise i will be updated by j),
66 so we can start from j+k+1
67 */
68 j = j+k+1;
69 k = 0;
70 }
71 }
72
73 return s.substr(i);
74 }
75};
Cost