Let's say you have a string S and a sequence of digits in a list L such that len(S) = len(L).
What would be the cleanest way of checking if you can find a bijection between the characters of the string to the digits in the sequence such that each character matches to one and only one digit.
For example, "aabbcc" should match with 115522 but not 123456 or 111111.
I have a complex setup with two dicts and loop, but I'm wondering if there's a clean way of doing this, maybe by using some function from the Python libraries.
or:
I would use a set for this:
The second set will have length equal to the first set if and only if the mapping is surjective. (if it is not, you will have two copies of a letter mapping to the same number in the second set, or vice versa)
Here is code that implements the idea
This will also return true if one sequence is shorter than the other, but a valid mapping has already been established. If the sequences must be the same length, you should add a check for that.
There's a more elegant way to do this (with sorting and
itertools.groupby
), but I'm wayy to sleep-deproved to figure that out right now. But this should still work:Hope this helps
Since you normally only talk about bijections between sets, I assume, unlike the other answers, that the order of the digits need not match the order of the letters. If so, there's a short, elegant solution, but it requires the
collections.Counter
class, which was introduced in python 2.7. For those stuck with an older version, there's a backport for 2.5+.Testing:
Your examples are rather light on the edge cases, because another way of reading your question allows for the number of digits and number of letters to be unequal (i.e. you look for a bijection from the set of unique characters to the set of unique digits, so e.g.
"aabbcc"
would biject onto"123333"
.). If this is what you meant, use this version instead:This respects the order: