Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 737 Bytes

942.增减字符串匹配.md

File metadata and controls

40 lines (34 loc) · 737 Bytes

942.增减字符串匹配

/*
 * @lc app=leetcode.cn id=942 lang=typescript
 *
 * [942] 增减字符串匹配
 */

// @lc code=start
function diStringMatch(s: string): number[] {}
// @lc code=end

解法 1: 贪心

function diStringMatch(s: string): number[] {
  let n = s.length,
    res: number[] = [],
    min = 0,
    max = n
  for (let i = 0; i <= n; i++) {
    res.push(s[i] === 'I' ? min++ : max--)
  }
  return res
}

Case

test.each([
  { input: { s: 'IDID' }, output: [0, 4, 1, 3, 2] },
  { input: { s: 'III' }, output: [0, 1, 2, 3] },
  { input: { s: 'DDI' }, output: [3, 2, 0, 1] },
])('input: s = $input.s', ({ input: { s }, output }) => {
  expect(diStringMatch(s)).toEqual(output)
})