Skip to content

Latest commit

 

History

History
39 lines (33 loc) · 1020 Bytes

1684.统计一致字符串的数目.md

File metadata and controls

39 lines (33 loc) · 1020 Bytes

1684.统计一致字符串的数目

/*
 * @lc app=leetcode.cn id=1684 lang=typescript
 *
 * [1684] 统计一致字符串的数目
 */

// @lc code=start
function countConsistentStrings(allowed: string, words: string[]): number {}
// @lc code=end

解法 1: 哈希表

function countConsistentStrings(allowed: string, words: string[]): number {
  let res = 0,
    set = new Set(allowed)
  next: for (let word of words) {
    for (let ch of word) if (!set.has(ch)) continue next
    res++
  }
  return res
}

Case

test.each([
  { input: { allowed: 'ab', words: ['ad', 'bd', 'aaab', 'baa', 'badab'] }, output: 2 },
  { input: { allowed: 'abc', words: ['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc'] }, output: 7 },
  { input: { allowed: 'cad', words: ['cc', 'acd', 'b', 'ba', 'bac', 'bad', 'ac', 'd'] }, output: 4 },
])('input: allowed = $input.allowed, words = $input.words', ({ input: { allowed, words }, output }) => {
  expect(countConsistentStrings(allowed, words)).toEqual(output)
})