Isomorphic Strings

Medium Strings
Two strings are isomorphic if characters in one can be consistently replaced to get the other (a one-to-one mapping in both directions).

Sample input

s = "egg", t = "add"

Sample output

true

Solution

def is_isomorphic(s, t):
    if len(s) != len(t):
        return False
    map_st, map_ts = {}, {}
    for a, b in zip(s, t):
        if a in map_st and map_st[a] != b:
            return False
        if b in map_ts and map_ts[b] != a:
            return False
        map_st[a] = b
        map_ts[b] = a
    return True

print(is_isomorphic("egg", "add"))
function isIsomorphic(s, t) {
  if (s.length !== t.length) return false;
  const mapST = new Map(), mapTS = new Map();
  for (let i = 0; i < s.length; i++) {
    const a = s[i], b = t[i];
    if (mapST.has(a) && mapST.get(a) !== b) return false;
    if (mapTS.has(b) && mapTS.get(b) !== a) return false;
    mapST.set(a, b);
    mapTS.set(b, a);
  }
  return true;
}

console.log(isIsomorphic("egg", "add"));
import java.util.HashMap;
import java.util.Map;

public class Main {
    static boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()) return false;
        Map<Character, Character> mapST = new HashMap<>(), mapTS = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char a = s.charAt(i), b = t.charAt(i);
            if (mapST.containsKey(a) && mapST.get(a) != b) return false;
            if (mapTS.containsKey(b) && mapTS.get(b) != a) return false;
            mapST.put(a, b);
            mapTS.put(b, a);
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isIsomorphic("egg", "add"));
    }
}
fun isIsomorphic(s: String, t: String): Boolean {
    if (s.length != t.length) return false
    val mapST = HashMap<Char, Char>()
    val mapTS = HashMap<Char, Char>()
    for (i in s.indices) {
        val a = s[i]
        val b = t[i]
        if (mapST[a] != null && mapST[a] != b) return false
        if (mapTS[b] != null && mapTS[b] != a) return false
        mapST[a] = b
        mapTS[b] = a
    }
    return true
}

fun main() {
    println(isIsomorphic("egg", "add"))
}
func isIsomorphic(_ s: String, _ t: String) -> Bool {
    if s.count != t.count { return false }
    var mapST = [Character: Character]()
    var mapTS = [Character: Character]()
    for (a, b) in zip(s, t) {
        if let mapped = mapST[a], mapped != b { return false }
        if let mapped = mapTS[b], mapped != a { return false }
        mapST[a] = b
        mapTS[b] = a
    }
    return true
}

print(isIsomorphic("egg", "add"))
bool isIsomorphic(String s, String t) {
  if (s.length != t.length) return false;
  final mapST = <String, String>{};
  final mapTS = <String, String>{};
  for (var i = 0; i < s.length; i++) {
    final a = s[i];
    final b = t[i];
    if (mapST.containsKey(a) && mapST[a] != b) return false;
    if (mapTS.containsKey(b) && mapTS[b] != a) return false;
    mapST[a] = b;
    mapTS[b] = a;
  }
  return true;
}

void main() {
  print(isIsomorphic("egg", "add"));
}
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

bool isIsomorphic(const string& s, const string& t) {
    if (s.size() != t.size()) return false;
    unordered_map<char, char> mapST, mapTS;
    for (size_t i = 0; i < s.size(); i++) {
        char a = s[i], b = t[i];
        if (mapST.count(a) && mapST[a] != b) return false;
        if (mapTS.count(b) && mapTS[b] != a) return false;
        mapST[a] = b;
        mapTS[b] = a;
    }
    return true;
}

int main() {
    cout << (isIsomorphic("egg", "add") ? "true" : "false") << endl;
    return 0;
}
#include <stdio.h>
#include <string.h>

int isIsomorphic(const char *s, const char *t) {
    if (strlen(s) != strlen(t)) return 0;
    int mapST[256] = {0}, mapTS[256] = {0};
    for (int i = 0; s[i]; i++) {
        unsigned char a = s[i], b = t[i];
        if (mapST[a] && mapST[a] != b) return 0;
        if (mapTS[b] && mapTS[b] != a) return 0;
        mapST[a] = b;
        mapTS[b] = a;
    }
    return 1;
}

int main() {
    printf("%s\n", isIsomorphic("egg", "add") ? "true" : "false");
    return 0;
}