LeetCode: Vowel Spellchecker Solution

1/**
2 * @param {string[]} wordlist
3 * @param {string[]} queries
4 * @return {string[]}
5 */
6var spellchecker = function (wordlist, queries) {
7 const vowelRegex = new RegExp("[aeiou]", "gi")
8 const replaceVowel = str => str.toLowerCase().replace(vowelRegex, "_")
9
10 const hashTable0 = wordlist.reduce(
11 (acc, word) => acc.set(word, word),
12 new Map()
13 )
14 const hashTable1 = wordlist.reduceRight(
15 (acc, word) => acc.set(word.toLowerCase(), word),
16 new Map()
17 )
18 const hashTable2 = wordlist.reduceRight(
19 (acc, word) => acc.set(replaceVowel(word), word),
20 new Map()
21 )
22
23 return queries.map(query => {
24 return (
25 hashTable0.get(query) ||
26 hashTable1.get(query.toLowerCase()) ||
27 hashTable2.get(replaceVowel(query)) ||
28 ""
29 )
30 })
31}

Comments

Loading comments...

Tags

leetcode

hash table

string

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: 3sum With Multiplicity

HoningJS

Search Posts