
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Number of Equivalent Domino Pairs in Python
Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0 <= i < j < length of Dominos, and determine D[i] is equivalent to D[j]. So if the domino list is like [[1, 2], [2, 1], [3, 4], [6, 5]]. The output will be 1
To solve this, we will follow these steps −
- let answer = 0
- for each pair p in dominos list −
- sort pair p
- Then store the frequency of each domino into D
- for b in values in D −
- answer := answer + (b * (b - 1))/2
- return answer
Example
Let us see the following implementation to get better understanding −
class Solution(object): def numEquivDominoPairs(self, dominoes): d = {} ans = 0 for i in dominoes: i.sort() i = tuple(i) if i not in d: d[i]= 1 else: d[i]+=1 for b in d.values(): ans += ((b*(b-1))//2) return ans ob1 = Solution() print(ob1.numEquivDominoPairs([[1,2],[2,1],[3,4],[5,6], [4,3]]))
Input
[[1,2],[2,1],[3,4],[5,6],[4,3]]
Output
2
- Related Articles
- Program to maximize the number of equivalent pairs after swapping in Python
- Domino Covering Board in Python
- Program to check string is palindrome or not with equivalent pairs in Python
- Program to find number of good pairs in Python
- Program to find max number of K-sum pairs in Python
- Domino and Tromino Tiling in C++
- Program to find out the number of pairs of equal substrings in Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Groups of Special-Equivalent Strings in Python
- Program to count number of fraction pairs whose sum is 1 in python
- Program to find number of good leaf nodes pairs using Python
- Count number of pairs (A
- Number of pairs with maximum sum in C++
- Index Pairs of a String in Python
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python

Advertisements