Isomorphic Runes: One-to-One Mapping
๐ฆ Isomorphic Runes: One-to-One Mapping
Difficulty: Beginner Tags: hashmap, strings, bijection, pattern-matching Series: CS 102: Problem Solving & Logic
Problem
In the mystical world of runes, two inscriptions are considered isomorphic if one can be transformed into the other through a consistent one-to-one character mapping.
Given two strings s and t, determine if they are isomorphic. Strings are isomorphic if characters in s can be replaced to get t, where:
- Each character in s maps to exactly one character in t
- No two characters in s map to the same character in t
- The mapping must be bijective (one-to-one in both directions)
Real-World Application
Isomorphic string checking appears in: - Data deduplication - identifying duplicate records with different formats - Pattern matching - finding similar structures in text or code - Schema mapping - aligning database schemas or API formats - Word games - pattern-based word puzzles - Compiler design - variable renaming and canonicalization
Input
data = {
's': str, # First string
't': str # Second string (same length as s)
}
Output
bool # True if isomorphic, False otherwise
Constraints
|s| = |t|(strings have equal length)|s|, |t| โค 100,000- Strings contain ASCII characters
Examples
Example 1: Basic Isomorphic Strings
Input: {'s': 'egg', 't': 'add'}
Output: True
Explanation: - 'e' โ 'a' (first char maps) - 'g' โ 'd' (second char maps) - 'g' โ 'd' (third char must map same as second) - Mapping: {'e': 'a', 'g': 'd'} โ - No conflicts, bijection maintained
Example 2: Non-Isomorphic Strings
Input: {'s': 'foo', 't': 'bar'}
Output: False
Explanation: - 'f' โ 'b' (first char maps) - 'o' โ 'a' (second char maps) - 'o' โ 'r' (CONFLICT! 'o' already mapped to 'a') - The mapping is inconsistent
Example 3: Bijection Violation
Input: {'s': 'badc', 't': 'baba'}
Output: False
Explanation: - 'b' โ 'b', 'a' โ 'a', 'd' โ 'b', 'c' โ 'a' - Two different characters ('b' and 'd') both map to 'b' โ Not one-to-one! - Two different characters ('a' and 'c') both map to 'a' โ Not one-to-one!
Example 4: Same Character Repeated
Input: {'s': 'paper', 't': 'title'}
Output: True
Explanation: - 'p' โ 't', 'a' โ 'i', 'p' โ 't' (consistent!), 'e' โ 'l', 'r' โ 'e' - Mapping: {'p': 't', 'a': 'i', 'e': 'l', 'r': 'e'} โ - Reverse mapping also valid: {'t': 'p', 'i': 'a', 'l': 'e', 'e': 'r'} โ
What You'll Learn
- How to maintain bidirectional (bijective) mappings using two hash maps
- When to use early exit strategies to optimize performance
- Pattern recognition in string transformations
- The difference between mapping and bijection
Why This Matters
Technical interviews frequently test your ability to recognize when bijection is required vs simple mapping, and how to efficiently track both directions of a relationship.
Starter Code
def challenge_function(data):
"""
Determine if two strings are isomorphic.
Args:
data: dict with keys 's' (str) and 't' (str)
Returns:
bool: True if s and t are isomorphic, False otherwise
"""
# Your implementation here
pass