Python - Check possible bijection between a sequence of characters and digits

In mathematics, a bijection refers to a function that establishes a one-to-one correspondence between two sets. Each element in one set has a unique and distinct counterpart in the other set, with no duplicates or missing elements in the mapping.

In Python, checking for a bijection involves validating whether a one-to-one mapping exists between elements of two sequences. For character-digit sequences, we need to ensure each character maps to exactly one digit and vice versa.

Algorithm for Checking Bijection

To check for a possible bijection between a sequence of characters and digits, follow these steps:

  • Check equal lengths Both sequences must have the same length for a bijection to exist.

  • Create mapping dictionaries Initialize two dictionaries to track character-to-digit and digit-to-character mappings.

  • Validate unique mappings Iterate through both sequences simultaneously and ensure no element maps to multiple counterparts.

  • Verify one-to-one correspondence Check that both dictionaries have equal lengths, confirming bidirectional uniqueness.

Method 1: Using Two Sequences

This approach checks bijection between separate character and digit sequences ?

def check_bijection(characters, digits):
    if len(characters) != len(digits):
        return False
    
    char_to_digit = {}
    digit_to_char = {}
    
    for char, digit in zip(characters, digits):
        if char in char_to_digit or digit in digit_to_char:
            return False
        char_to_digit[char] = digit
        digit_to_char[digit] = char
    
    return len(char_to_digit) == len(digit_to_char)

# Test with valid bijection
characters = ['a', 'b', 'c']
digits = [1, 2, 3]
print("Valid bijection:", check_bijection(characters, digits))

# Test with invalid bijection (duplicate digit)
characters = ['a', 'b', 'c']
digits = [1, 2, 2]
print("Invalid bijection:", check_bijection(characters, digits))
Valid bijection: True
Invalid bijection: False

Method 2: Using Single Sequence with Range

This approach maps characters in a sequence to consecutive digits starting from 0 ?

def is_bijection_with_range(sequence):
    char_to_digit = {}
    digit_to_char = {}
    
    for i, char in enumerate(sequence):
        digit = i  # Map to consecutive digits 0, 1, 2, ...
        
        if char in char_to_digit and char_to_digit[char] != digit:
            return False
        if digit in digit_to_char and digit_to_char[digit] != char:
            return False
            
        char_to_digit[char] = digit
        digit_to_char[digit] = char
    
    return True

# Test with unique characters
print("Unique characters:", is_bijection_with_range("abc"))

# Test with duplicate characters
print("Duplicate characters:", is_bijection_with_range("abb"))
Unique characters: True
Duplicate characters: False

Method 3: Using Sets for Validation

A simpler approach using set comparison to check uniqueness ?

def check_bijection_simple(seq1, seq2):
    if len(seq1) != len(seq2):
        return False
    
    # Check if both sequences have unique elements
    return len(set(seq1)) == len(seq1) and len(set(seq2)) == len(seq2)

# Test examples
chars = ['x', 'y', 'z']
nums = [7, 8, 9]
print("Simple check - unique:", check_bijection_simple(chars, nums))

chars_dup = ['x', 'y', 'x']
nums_dup = [7, 8, 7]
print("Simple check - duplicates:", check_bijection_simple(chars_dup, nums_dup))
Simple check - unique: True
Simple check - duplicates: False

Comparison

Method Use Case Time Complexity Space Complexity
Two Dictionaries Custom mapping validation O(n) O(n)
Range Mapping Sequential digit assignment O(n) O(n)
Set Comparison Simple uniqueness check O(n) O(n)

Conclusion

Use dictionary mapping for validating specific character-digit bijections. For simple uniqueness checking, set comparison provides a cleaner solution. Choose the method based on whether you need custom mappings or just uniqueness validation.

---
Updated on: 2026-03-27T11:27:32+05:30

414 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements