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.
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.
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.