Requires thinking if you want to implement set difference or intersection. I just figured depending on needs using existing built-in types may be fun.
Requires thinking if you want to implement set difference or intersection. I just figured depending on needs using existing built-in types may be fun.
For the problem we don't care how many instances of each letter occurs. We just need to know if there is 0 or 1 instance of a given letter. A set is a pretty good way to do this. Store a set of each "compartment", intersect the two sets, and you should have one value.
We can do the same thing except instead of a set we use an integer and set bits. a-z are bits 0-26 and A_Z are bits 27-52. We can set a bit with bitwise-or. We can intersect two masks with bitwise-and.
My code is probably a little too verbose. I'm not super familiar with python so I wasn't sure the best way to deal with characters and integers. But it's functional.
for key, _ := range first {
if _, found2 := second[key]; found2 {
if _, found3 := third[key]; found3 {
// This is the intersection of 3 sets
}
}
}