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.
longwinningLotteryTicket(vector<string>tickets){// A map to store the frequency of each bitmaskunordered_map<int,long>bitmask_count;// Process each ticket and compute its bitmaskfor(conststring&ticket:tickets){intbitmask=0;// Mark the digits that appear in the ticketfor(charc:ticket){bitmask|=(1<<(c-'0'));}// Increment the count for this bitmaskbitmask_count[bitmask]++;}longresult=0;// Now check pairs of bitmasksfor(autoit1=bitmask_count.begin();it1!=bitmask_count.end();++it1){for(autoit2=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 samefor(autoit1=bitmask_count.begin();it1!=bitmask_count.end();++it1){if((it1->first|it1->first)==1023){result+=(it1->second*(it1->second-1))/2;}}returnresult;}
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 →
in C++