Skip to content

Commit 42e754c

Browse files
authored
Create 1163-last-substring-in-lexicographical-order.js
1 parent 68bbf67 commit 42e754c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const lastSubstring = function(s) {
6+
let ans = '',
7+
max = 'a'
8+
for (let i = 0; i < s.length; ) {
9+
let j = i,
10+
sub = s.slice(i)
11+
if (max < s[i] || ans < sub) {
12+
max = s[i]
13+
ans = sub
14+
}
15+
while (i < s.length && s[i + 1] === s[i]) {
16+
i++
17+
}
18+
if (j === i) {
19+
i++
20+
}
21+
}
22+
return ans
23+
}

0 commit comments

Comments
 (0)