LeetCode: Isomorphic Strings Solution
Use 2 hash tablesApproach
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.length56 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 continue11 }1213 if (map.get(s[i]) !== t[i]) {14 return false15 }16 }1718 return true19}
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.