tutorialcup
Number of Equivalent Domino Pairs Leetcode Solution
Problem statement
In the problem " Number of Equivalent Domino Pairs," we are given a list of dominoes where each domino consists of two values like dominoes=. Two dominoes, dominoes = and dominoes= are equivalent if (a==c and b==d) or (a==d and c==d).
Our task is to find out the total number of pair(i,j) where i!=j and dominoes is equivalent to dominoes. Given values of a and b lies in the range.
Example
dominoes = ,,,]
1
Explanation: In this example dominoes is equivalent to dominoes because it is satisfying the condition (a==d and c==d). As this is the only pair satisfying equivalent domino criteria so the answer is one.
Approach for Number of Equivalent Domino Pairs Leetcode Solution
We can solve this problem by selecting a domino and then checking all other remaining dominoes if they are equivalent to the selected domino or not. we will do this for all dominoes and then divide the total count of equivalent dominoes by 2 will give the answer. But the time complexity for this approach would be O(n*n). We can solve this problem in O(n) time by following the below steps:
- We will convert two numbers (a,b) into a single number, this will make checking equivalence easy.
- This hash function will convert two numbers into one number c=min(a,b)*10+max(a,b).
- Now we will create a hash table to count the frequency of each number.
- We will traverse the dominoes array and will store the frequency of c in the hash table.
www.tutorialcup.com/leetcode-solutions/number-of-equivale...
Number of Equivalent Domino Pairs Leetcode Solution
Problem statement
In the problem " Number of Equivalent Domino Pairs," we are given a list of dominoes where each domino consists of two values like dominoes=. Two dominoes, dominoes = and dominoes= are equivalent if (a==c and b==d) or (a==d and c==d).
Our task is to find out the total number of pair(i,j) where i!=j and dominoes is equivalent to dominoes. Given values of a and b lies in the range.
Example
dominoes = ,,,]
1
Explanation: In this example dominoes is equivalent to dominoes because it is satisfying the condition (a==d and c==d). As this is the only pair satisfying equivalent domino criteria so the answer is one.
Approach for Number of Equivalent Domino Pairs Leetcode Solution
We can solve this problem by selecting a domino and then checking all other remaining dominoes if they are equivalent to the selected domino or not. we will do this for all dominoes and then divide the total count of equivalent dominoes by 2 will give the answer. But the time complexity for this approach would be O(n*n). We can solve this problem in O(n) time by following the below steps:
- We will convert two numbers (a,b) into a single number, this will make checking equivalence easy.
- This hash function will convert two numbers into one number c=min(a,b)*10+max(a,b).
- Now we will create a hash table to count the frequency of each number.
- We will traverse the dominoes array and will store the frequency of c in the hash table.
www.tutorialcup.com/leetcode-solutions/number-of-equivale...