Check Character Mapping Consistency
StringsAuto-graded · 24 test casesLast updated
Tests whether two strings establish a valid one-to-one character correspondence at each position.
You are given two strings text1 and text2 containing printable ASCII characters. Your task is to determine whether these strings define a valid character-to-character mapping where:
- Each character in
text1consistently maps to exactly one character intext2throughout the entire string - No two different characters in
text1map to the same character intext2 - The mapping is only valid if both strings have identical length
Write a function that returns True if such a valid mapping exists, and False otherwise.
Important: The mapping is position-based. If text1[i] == text1[j], then text2[i] must equal text2[j]. Conversely, if text1[i] != text1[j], then text2[i] must not equal text2[j].
Constraints
0 <= len(text1), len(text2) <= 10^5- Both strings contain only printable ASCII characters (ASCII values 32-126)
- Character comparisons are case-sensitive (
'A'and'a'are treated as different characters)
Examples
Example 1
Input: has_valid_mapping('paper', 'title')
Output: True
Example 2
Input: has_valid_mapping('foo', 'bar')
Output: False
Example 3
Input: has_valid_mapping('ab', 'aa')
Output: False
Example 4
Input: has_valid_mapping('Send $100!', 'Wfoe %211?')
Output: True
Hints
Hint 1
If the strings have different lengths, can any position-based mapping be valid?
Hint 2
Track what each character in text1 should map to. If you see the same character again, does it map to the same target?
Hint 3
A single mapping dictionary isn't enough—you also need to prevent two different source characters from mapping to the same destination character.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that a valid mapping requires maintaining two separate invariants:
-
Consistency (Function Property): Each character in
text1must always map to the same character intext2. When we see charactercintext1multiple times, it must correspond to the same character intext2at every occurrence. -
Uniqueness (Injection Property): No two distinct characters in
text1can map to the same character intext2. This ensures the mapping is reversible.
We use two hash maps to track both directions simultaneously:
forward: maps each character fromtext1to its corresponding character intext2reverse: maps each character fromtext2back to its source character intext1
As we iterate through both strings in parallel, we check:
- If we've seen the current
text1character before, verify it maps to the sametext2character - If we've seen the current
text2character before, verify it came from the sametext1character
If either check fails, the mapping is invalid. If we complete the iteration without conflicts, the mapping is valid.
Why This Works
Using only one map would catch repeated characters that change their target, but would miss the case where multiple different characters try to map to the same target. For example, with text1 = "ab" and text2 = "aa", a forward-only map would allow both 'a' → 'a' and 'b' → 'a', making decryption impossible.
The reverse map catches this collision: when processing ('b', 'a'), we find that 'a' is already claimed by a different source character.
Both empty strings trivially satisfy the requirement (empty mapping is valid). Length mismatch is caught immediately since position-based alignment is impossible.
Complexity
-
Time Complexity: O(n) where n is the length of the strings. We make a single pass through both strings with constant-time hash map operations at each position.
-
Space Complexity: O(1) auxiliary space. While we use two hash maps, they can contain at most 95 entries each (one per printable ASCII character), which is a constant regardless of input size.
Solution
def has_valid_mapping(text1: str, text2: str) -> bool:
if len(text1) != len(text2):
return False
forward = {}
reverse = {}
for a, b in zip(text1, text2):
if a in forward and forward[a] != b:
return False
if b in reverse and reverse[b] != a:
return False
forward[a] = b
reverse[b] = a
return True