code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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