5 Best Ways to Check If One String Can Be 1-to-1 Mapped Into Another String in Python

Rate this post

πŸ’‘ Problem Formulation: We need to check if characters from one string can be mapped one-to-one with the characters of another string. Essentially, we want to ensure that each character in the first string can be replaced with a unique character in the second string without any overlaps or repetitions. For example, “abc” can be 1-to-1 mapped into “bcd”, but “foo” cannot be 1-to-1 mapped into “bar” because ‘o’ would have to map to two different characters.

Method 1: Using Dictionary Mapping

This method involves creating a dictionary to keep track of the character mappings from the first string to the second. We iterate through the strings and map each character from the first string to the corresponding character in the second string. If the mapping is consistent and each character maps to a unique character, the function returns True, otherwise False.

Here’s an example:

def is_one_to_one_mapped(str1, str2):
    mapping_dict = {}
    for char1, char2 in zip(str1, str2):
        if char1 not in mapping_dict:
            if char2 in mapping_dict.values():
                return False
            mapping_dict[char1] = char2
        elif mapping_dict[char1] != char2:
            return False
    return True

print(is_one_to_one_mapped("abc", "bcd")) # Output: True
print(is_one_to_one_mapped("foo", "bar")) # Output: False

The function is_one_to_one_mapped() checks if each character in ‘abc’ can be replaced by a corresponding character in ‘bcd’, which is possible, thus returning True. In contrast, ‘foo’ to ‘bar’ mapping fails because ‘o’ would need to map to both ‘a’ and ‘r’ simultaneously, coming back with False.

Method 2: Set and Length Comparison

Another approach is to compare the lengths of sets of the characters of both strings. If the set length of the first string equals that of the second string, then a 1-to-1 mapping is possible. This method is best when two strings are of the same length and consist of unique characters.

Here’s an example:

def can_map_str1_to_str2(str1, str2):
    return len(set(str1)) == len(set(str2))

print(can_map_str1_to_str2("abc", "def")) # Output: True
print(can_map_str1_to_str2("aab", "xyz")) # Output: False

The function can_map_str1_to_str2() returns True if the unique count of characters in both strings is the same. ‘abc’ to ‘def’ returns True, whereas ‘aab’ to ‘xyz’ returns False due to the repetition of ‘a’ in the first string.

Method 3: Using ASCII Values

This technique employs the ASCII values of the characters by converting them to ASCII, and then checking for one-to-one correspondence. It might not be practical for unicode strings, but works fine for ASCII strings.

Here’s an example:

def is_mapped_by_ascii(str1, str2):
    return len({ord(c) for c in str1}) == len({ord(c) for c in str2})

print(is_mapped_by_ascii("cat", "dog"))  # Output: True
print(is_mapped_by_ascii("cat", "fish")) # Output: False

The function is_mapped_by_ascii() compares character uniqueness based on ASCII values. The strings “cat” and “dog” can be mapped 1-to-1, hence True. But “cat” cannot be mapped to “fish” due to a different number of unique characters, resulting in False.

Method 4: Using a Bijective Function

We can conceptualize our strings as sets and try to establish a bijective function (one-to-one and onto) between these sets. A bijective function suggests that each character maps to a unique character and covers all characters of the second string exactly once.

Here’s an example:

def is_bijective(str1, str2):
    return len(str1) == len(str2) and len(set(zip(str1, str2))) == len(set(str1))

print(is_bijective("paper", "title")) # Output: True
print(is_bijective("deed", "seed"))  # Output: False

The function is_bijective() first ensures that the lengths of both strings are equal. Then it creates a set of tuples representing the mapping and checks that the number of unique mappings is equal to the number of unique characters in the first string.

Bonus One-Liner Method 5: Using All and Set

This succinct approach returns True if every character in the first string maps uniquely to a character in the second string by converting the strings to sets and comparing lengths, all in one line.

Here’s an example:

print(all(len(set(s)) == len(set(t)) for s, t in zip("abcd", "efgh")))  # Output: True
print(all(len(set(s)) == len(set(t)) for s, t in zip("hello", "world"))) # Output: False

The one-liner uses all() in a generator expression to check each character’s uniqueness in two simultaneous iterations. The comparison works for “abcd” and “efgh”, but not for “hello” and “world”.


  • Method 1: Dictionary Mapping. Suitable for most cases. Handles non-unique character scenarios. Performance degrades on larger strings due to iteration and dictionary lookups.
  • Method 2: Set and Length Comparison. Simple and useful for strings with unique characters. It can return false positives when there are repetitions within both strings.
  • Method 3: Using ASCII Values. A quick comparison method, practical for ASCII strings, but not suitable for unicode or strings with repeating characters.
  • Method 4: Bijective Function. The most mathematically rigorous approach. Ensures a true bijective relationship between strings, but computation might be heavy on longer strings.
  • Method 5: One-Liner Using All and Set. Energy efficient and elegant. Best suited for short strings or quick checks, but not the most readable or debuggable.