Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 807 Bytes

2283.判断一个数的数字计数是否等于数位的值.md

File metadata and controls

40 lines (34 loc) · 807 Bytes

2283.判断一个数的数字计数是否等于数位的值

/*
 * @lc app=leetcode.cn id=2283 lang=typescript
 *
 * [2283] 判断一个数的数字计数是否等于数位的值
 */

// @lc code=start
function digitCount(num: string): boolean {}
// @lc code=end

解法 1: 计数

function digitCount(num: string): boolean {
  const cnt: number[] = []
  for (let ch of num) {
    const i = Number(ch)
    cnt[i] = (cnt[i] ?? 0) + 1
  }
  for (let i = 0; i < num.length; i++) {
    if ((cnt[i] ?? 0) !== Number(num[i])) return false
  }
  return true
}

Case

test.each([
  { input: { num: '1210' }, output: true },
  { input: { num: '030' }, output: false },
])('input: num = $input.num', ({ input: { num }, output }) => {
  expect(digitCount(num)).toEqual(output)
})