• + 0 comments

    in C++

    long winningLotteryTicket(vector<string> tickets) {
        // A map to store the frequency of each bitmask
        unordered_map<int, long> bitmask_count;
        
        // Process each ticket and compute its bitmask
        for (const string& ticket : tickets) {
            int bitmask = 0;
            
            // Mark the digits that appear in the ticket
            for (char c : ticket) {
                bitmask |= (1 << (c - '0'));
            }
            
            // Increment the count for this bitmask
            bitmask_count[bitmask]++;
        }
        
        long result = 0;
        
        // Now check pairs of bitmasks
        for (auto it1 = bitmask_count.begin(); it1 != bitmask_count.end(); ++it1) {
            for (auto it2 = next(it1); it2 != bitmask_count.end(); ++it2) {
                if ((it1->first | it2->first) == 1023) {
                    result += it1->second * it2->second;
                }
            }
        }
        
        // Check the cases where both bitmasks are the same
        for (auto it1 = bitmask_count.begin(); it1 != bitmask_count.end(); ++it1) {
            if ((it1->first | it1->first) == 1023) {
                result += (it1->second * (it1->second - 1)) / 2;
            }
        }
    
        return result;
    }