code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

205 Isomorphic Strings

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const isomorphicStrings = (s, t) => {
  let smap = {}
  let tmap = {}
  for (let i = 0; i < s.length; i++) {
    if ((smap[s[i]] && smap[s[i]] !== t[i]) || (tmap[t[i]] && tmap[t[i]] != s[i])) {
      return false
    }
    smap[s[i]] = t[i]
    tmap[t[i]] = s[i]
  }
  return true
}

console.log(isomorphicStrings('egg', 'add'))

Created 2022-03-21T08:44:21+00:00 · Edit