We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def winningLotteryTicket(tickets):
# Convert each ticket to a bitmask
freq = {}
FULL_MASK = (1 << 10) - 1 # 1023 = 0b1111111111
for ticket in tickets:
mask = 0
for digit in ticket:
mask |= 1 << int(digit)
freq[mask] = freq.get(mask, 0) + 1
masks = list(freq.keys())
count = 0
for i in range(len(masks)):
for j in range(i, len(masks)):
if (masks[i] | masks[j]) == FULL_MASK:
if i == j:
# pairs from the same mask: nC2 = n*(n-1)//2
count += freq[masks[i]] * (freq[masks[i]] - 1) // 2
else:
# pairs from different masks
count += freq[masks[i]] * freq[masks[j]]
return count
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Winning Lottery Ticket
You are viewing a single comment's thread. Return to all comments →
def winningLotteryTicket(tickets): # Convert each ticket to a bitmask freq = {} FULL_MASK = (1 << 10) - 1 # 1023 = 0b1111111111