LeetCode: Isomorphic Strings Solution

Use 2 hash tables

Approach

Use two hash tables

  • one for mapping char to char
  • one for avoiding the case of 2 char both mapping to 1 char

Implementation

1var isIsomorphic = function (s, t) {
2 const map = new Map()
3 const replaced = new Set()
4 const n = s.length
5
6 for (let i = 0; i < n; i++) {
7 if (map.has(s[i]) === false && replaced.has(t[i]) === false) {
8 map.set(s[i], t[i])
9 replaced.add(t[i])
10 continue
11 }
12
13 if (map.get(s[i]) !== t[i]) {
14 return false
15 }
16 }
17
18 return true
19}

Comments

Loading comments...

Tags

leetcode

hash map

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

CSSBattle 1.8: Forking Crazy

Position, translate

Previous Post

LeetCode: Find Median from Data Stream

Insert and keep the array sorted

HoningJS

Search Posts