Skip to content

Latest commit

 

History

History
38 lines (32 loc) · 677 Bytes

1163.按字典序排在最后的子串.md

File metadata and controls

38 lines (32 loc) · 677 Bytes

1163.按字典序排在最后的子串

/*
 * @lc app=leetcode.cn id=1163 lang=typescript
 *
 * [1163] 按字典序排在最后的子串
 */

// @lc code=start
function lastSubstring(s: string): string {}
// @lc code=end

解法 1: 暴力

function lastSubstring(s: string): string {
  const n = s.length
  let res = ''
  for (let i = 0; i < n; i++) {
    const a = s.slice(i, n)
    if (res < a) res = a
  }
  return res
}

Case

test.each([
  { input: { s: 'abab' }, output: 'bab' },
  { input: { s: 'leetcode' }, output: 'tcode' },
])('input: s = $input.s', ({ input: { s }, output }) => {
  expect(lastSubstring(s)).toEqual(output)
})