# Python | Check possible bijection between sequence of characters and digits

Last Updated : 13 Apr, 2023

Given a string ‘char_seq'(sequence of characters) and a positive integer ‘dig_seq'(sequence of digits), Write a Python program to find possible bijection or one-one onto relationship between ‘char_seq’ and ‘dig_seq’ such that each character matches to one and only one digit.

Examples:

```Input : char_seq = 'bxdyxb'
dig_seq = 123421
Output : True

Input : char_seq = 'bxdyxb'
dig_seq = 123321
Output : False```

Method #1: Using zip method This method simply zips the ‘char_seq’ and ‘dig_seq’ and checks if corresponding digits and characters matches or not.

## Python3

 `# Python3 program to Check possible bijection ` `# between sequence of characters and digits`   `def` `is_bijection(char_seq, dig_seq):` `    ``z ``=` `zip``(``str``(char_seq), ``str``(dig_seq))` `    ``res ``=` `all``(` `    ``(z1[``0``] ``=``=` `z2[``0``]) ``=``=` `(z1[``1``] ``=``=` `z2[``1``]) ``for` `z1 ``in` `z ``for` `z2 ``in` `z)` `    `  `    ``return` `res` `    `  `# Driver code` `char_seq ``=` `'bxdyxb'` `dig_seq ``=` `123421` `print``(is_bijection(char_seq, dig_seq))`

Output

`True`

Method #2: Using itertools.groupby method This method uses the same approach with a slight difference, it uses itertools.groupby to match characters with digits.

## Python3

 `# Python3 program to Check possible bijection ` `# between sequence of characters and digits` `import` `itertools`   `def` `is_bijection(char_seq, dig_seq):` `    ``z ``=` `sorted``(``zip``(``str``(char_seq), ``str``(dig_seq)))` `    ``res ``=` `all``(gx ``=``=` `gy` `          ``for` `k, g ``in` `itertools.groupby(z, key ``=` `lambda` `res: res[``0``])` `          ``for` `gx ``in` `g ``for` `gy ``in` `g)` `    `  `    ``return` `res` `    `  `# Driver code` `char_seq ``=` `'bxdyxb'` `dig_seq ``=` `123421` `print``(is_bijection(char_seq, dig_seq))`

Output

`True`

Method #3: It works by iterating through the input sequences and creating a dictionary that maps characters to digits. For each character and digit pair, if the character is already in the dictionary and the digit does not match the one already mapped to the character, then the bijection is not valid and the function returns False. Otherwise, the character and digit are added to the dictionary. If the loop completes, then the bijection is valid and the function returns True.

## Python3

 `def` `is_bijection(char_seq, dig_seq):` `    ``# Create a dictionary to map characters to digits` `    ``dig_seq``=``str``(dig_seq)` `    ``char_dict ``=` `{}` `    ``# Iterate through the character and digit pairs` `    ``for` `c, d ``in` `zip``(char_seq, dig_seq):` `        ``# If the character is already in the dictionary` `        ``if` `c ``in` `char_dict:` `            ``# If the digit does not match the one already mapped to the character,` `            ``# then the bijection is not valid` `            ``if` `char_dict !``=` `d:` `                ``return` `False` `        ``else``:` `            ``# Add the character and digit to the dictionary` `            ``char_dict ``=` `d` `    ``# If the loop completes, then the bijection is valid` `    ``return` `True`   `char_seq ``=` `'bxdyxb'` `dig_seq ``=` `123421` `print``(is_bijection(char_seq, dig_seq))` `#This code is contributed by Edula Vinay Kumar Reddy`

Output

`True`

Time complexity: O(n) and
Auxiliary space: O(n), where n is the length of the input sequences.

Method #4: By repeatedly going through the input sequences, it builds a dictionary that converts characters to numbers. The bijection is invalid and the function returns False for any character and digit pair where the character is already present in the dictionary and the digit does not match the one previously mapped to the character. The character and digit are added to the vocabulary in all other cases. The bijection is true and the function returns True if the loop is finished.

## Python

 `# Python program to Check possible bijection ` `# between sequence of characters and digits` `def` `is_bijection(char_seq, dig_seq):` `    ``dig_seq_str ``=` `str``(dig_seq)` `    ``if` `len``(char_seq) !``=` `len``(dig_seq_str):` `        ``return` `False`   `    ``char_to_dig ``=` `{}` `    ``dig_to_char ``=` `{}`   `    ``for` `char, dig ``in` `zip``(char_seq, dig_seq_str):` `        ``if` `char ``in` `char_to_dig ``and` `char_to_dig[char] !``=` `dig:` `            ``return` `False` `        ``if` `dig ``in` `dig_to_char ``and` `dig_to_char[dig] !``=` `char:` `            ``return` `False` `        ``char_to_dig[char] ``=` `dig` `        ``dig_to_char[dig] ``=` `char`   `    ``return` `True`   `# drive code` `char_seq ``=` `'bxdyxb'` `dig_seq ``=` `123421` `print``(is_bijection(char_seq, dig_seq))`

Output

`True`

Time Complexity : O(n)
Auxiliary Space: O(n)

Article Tags :
Practice Tags :