LeetCode: Verifying An Alien Dictionary Solution

1/**
2 * @param {string[]} words
3 * @param {string} order
4 * @return {boolean}
5 */
6var isAlienSorted = function (words, order) {
7 const N = words.length
8 order = order.split("").reduce((acc, char, i) => acc.set(char, i), new Map())
9 let sorted = true
10 for (let i = 0; i < N - 1; i++) {
11 for (let j = 0; j < words[i].length; j++) {
12 if (order.get(words[i][j]) === order.get(words[i + 1][j])) {
13 continue
14 } else if (order.get(words[i][j]) < order.get(words[i + 1][j])) {
15 break
16 } else {
17 sorted = false
18 }
19 }
20 }
21 return sorted
22}

Comments

Loading comments...

Tags

leetcode

hash table

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: Longest Increasing Path In A Matrix

Apr 10, 2021

HoningJS

Search Posts