Solution 1 - Two Maps
Maintain mappings in both directions to enforce a one-to-one character correspondence.
Maintain mappings in both directions to enforce a one-to-one character correspondence.
""" 0205.1 - Isomorphic Strings - Solution 1 - Two Maps """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isIsomorphic(self, s: str, t: str) -> bool:
"""Isomorphic Strings Function"""
a = {}
b = {}
for x, y in zip(s, t):
if a.get(x, y) != y or b.get(y, x) != x:
return False
a[x] = y
b[y] = x
return True
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
assert Solution().isIsomorphic("egg", "add")
assert not Solution().isIsomorphic("foo", "bar")
print("tests passed")
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()/** 0205.1 - Isomorphic Strings - Solution 1 - Two Maps */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
isIsomorphic(s, t) {
const a = new Map();
const b = new Map();
for (let i = 0; i < s.length; i++) {
const x = s[i];
const y = t[i];
if ((a.has(x) && a.get(x) !== y) || (b.has(y) && b.get(y) !== x)) {
return false;
}
a.set(x, y);
b.set(y, x);
}
return true;
}
}
console.log(new Solution().isIsomorphic("egg", "add")); // true/** 0205.1 - Isomorphic Strings - Solution 1 - Two Maps */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
isIsomorphic(s: string, t: string): boolean {
const a: Map<string, string> = new Map();
const b: Map<string, string> = new Map();
for (let i = 0; i < s.length; i++) {
const x: string = s[i];
const y: string = t[i];
if ((a.has(x) && a.get(x) !== y) || (b.has(y) && b.get(y) !== x)) {
return false;
}
a.set(x, y);
b.set(y, x);
}
return true;
}
}
console.log(new Solution().isIsomorphic("egg", "add")); // true// 0205.1 - Isomorphic Strings - Solution 1 - Two Maps
package main
/////////////////////////////////////////////////////////////////////////////////////
// Imports
/////////////////////////////////////////////////////////////////////////////////////
import "fmt"
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
func isIsomorphic(s string, t string) bool {
a := map[byte]byte{}
b := map[byte]byte{}
for i := 0; i < len(s); i++ {
x := s[i]
y := t[i]
if v, ok := a[x]; ok && v != y {
return false
}
if v, ok := b[y]; ok && v != x {
return false
}
a[x] = y
b[y] = x
}
return true
}
func main() {
fmt.Println(isIsomorphic("egg", "add"))
}// 0205.1 - Isomorphic Strings - Solution 1 - Two Maps
use std::collections::HashMap;
struct Solution;
impl Solution {
pub fn is_isomorphic(s: &str, t: &str) -> bool {
let mut a: HashMap<u8, u8> = HashMap::new();
let mut b: HashMap<u8, u8> = HashMap::new();
for (&x, &y) in s.as_bytes().iter().zip(t.as_bytes().iter()) {
if a.get(&x).is_some_and(|&v| v != y) || b.get(&y).is_some_and(|&v| v != x) {
return false;
}
a.insert(x, y);
b.insert(y, x);
}
true
}
}
fn main() {
println!("{}", Solution::is_isomorphic("egg", "add"));
}