Skip to content

Latest commit

 

History

History
48 lines (41 loc) · 1.04 KB

2325.解密消息.md

File metadata and controls

48 lines (41 loc) · 1.04 KB

2325.解密消息

/*
 * @lc app=leetcode.cn id=2325 lang=typescript
 *
 * [2325] 解密消息
 */

// @lc code=start
function decodeMessage(key: string, message: string): string {}
// @lc code=end

解法 1: 哈希表

function decodeMessage(key: string, message: string): string {
  let chs = [...new Set(key)].filter(ch => ch !== ' '),
    map = new Map<string, string>()
  for (let [i, ch] of chs.entries()) {
    map.set(ch, String.fromCharCode(i + 97))
  }

  let res = ''
  for (let ch of message) {
    res += map.get(ch) ?? ' '
  }
  return res
}

Case

test.each([
  {
    input: { key: 'the quick brown fox jumps over the lazy dog', message: 'vkbs bs t suepuv' },
    output: 'this is a secret',
  },
  {
    input: { key: 'eljuxhpwnyrdgtqkviszcfmabo', message: 'zwx hnfx lqantp mnoeius ycgk vcnjrdb' },
    output: 'the five boxing wizards jump quickly',
  },
])('input: key = $input.key, message = $input.message', ({ input: { key, message }, output }) => {
  expect(decodeMessage(key, message)).toEqual(output)
})