[C++/Java/Python] One pass - Clean & Concise - Strictly O(N)
\n(Picture from Leetcode)\n\nAlgorithm\n- Count the number of domino values in row A, named it cntA.\n- Count the number of domino values in row B, named it cnt
![image](https://assets.leetcode.com/users/images/5c559b56-92b1-4ce1-93dd-9779e0125b6b_1603097032.4023457.png)\n*(Picture from Leetcode)*\n\n**Algorithm**\n- Count the number of domino values in row A, named it `cntA`.\n- Count the number of domino values in row B, named it `cntB`.\n- Count the number of same domino values if `ith` domino in row A the same `ith` domino in row B, named it `cntSame`.\n- For `v` in `[1..6]` (six number of dominos)\n\t- If `cntA[v] + cntB[v] - cntSame[v] == n` // It means it\'s possible that we can swap `A` to `B` or `B` to `A` so that all the values in `A` are the same, or all the values in `B` are the same.\n\t\t- `minSwap = min(cntA[v], cntB[v]) - cntSame[v]` // Choose swap `A` to `B` if `cntA[v]` is smaller or swap `B` to `A` if `cnt[B]` is smaller\n\t\t- `ans = min(ans, minSwap)`\n\n<iframe src="https://leetcode.com/playground/5wCbXe3u/shared" frameBorder="0" width="100%" height="400"></iframe>\n\n**Complexity**\n- Time: `O(N)`\n- Space: `O(1)`\n\nFeel free to ask your quetions. If this post is helpful, please help to give it a **vote**!.\nHappy coding!
[]