Skip to content

Commit ce3d4b4

Browse files
committedApr 5, 2025
Add solution #1163
1 parent 84f5d1a commit ce3d4b4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,161 LeetCode solutions in JavaScript
1+
# 1,162 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -909,6 +909,7 @@
909909
1160|[Find Words That Can Be Formed by Characters](./solutions/1160-find-words-that-can-be-formed-by-characters.js)|Easy|
910910
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
911911
1162|[As Far from Land as Possible](./solutions/1162-as-far-from-land-as-possible.js)|Medium|
912+
1163|[Last Substring in Lexicographical Order](./solutions/1163-last-substring-in-lexicographical-order.js)|Hard|
912913
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
913914
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
914915
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 1163. Last Substring in Lexicographical Order
3+
* https://leetcode.com/problems/last-substring-in-lexicographical-order/
4+
* Difficulty: Hard
5+
*
6+
* Given a string s, return the last substring of s in lexicographical order.
7+
*/
8+
9+
/**
10+
* @param {string} s
11+
* @return {string}
12+
*/
13+
var lastSubstring = function(s) {
14+
let i = 0;
15+
let j = 1;
16+
let k = 0;
17+
const n = s.length;
18+
19+
while (j + k < n) {
20+
if (s[i + k] === s[j + k]) {
21+
k++;
22+
} else if (s[i + k] < s[j + k]) {
23+
i = Math.max(i + k + 1, j);
24+
j = i + 1;
25+
k = 0;
26+
} else {
27+
j += k + 1;
28+
k = 0;
29+
}
30+
}
31+
32+
return s.substring(i);
33+
};

0 commit comments

Comments
 (0)
Please sign in to comment.