<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>. A pair of indices <code>i</code>, <code>j</code> where <code>0 &lt;=&nbsp;i &lt; j &lt; nums.length</code> is called beautiful if the <strong>first digit</strong> of <code>nums[i]</code> and the <strong>last digit</strong> of <code>nums[j]</code> are <strong>coprime</strong>.</p>

<p>Return <em>the total number of beautiful pairs in </em><code>nums</code>.</p>

<p>Two integers <code>x</code> and <code>y</code> are <strong>coprime</strong> if there is no integer greater than 1 that divides both of them. In other words, <code>x</code> and <code>y</code> are coprime if <code>gcd(x, y) == 1</code>, where <code>gcd(x, y)</code> is the <strong>greatest common divisor</strong> of <code>x</code> and <code>y</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,5,1,4]
<strong>Output:</strong> 5
<strong>Explanation:</strong> There are 5 beautiful pairs in nums:
When i = 0 and j = 1: the first digit of nums[0] is 2, and the last digit of nums[1] is 5. We can confirm that 2 and 5 are coprime, since gcd(2,5) == 1.
When i = 0 and j = 2: the first digit of nums[0] is 2, and the last digit of nums[2] is 1. Indeed, gcd(2,1) == 1.
When i = 1 and j = 2: the first digit of nums[1] is 5, and the last digit of nums[2] is 1. Indeed, gcd(5,1) == 1.
When i = 1 and j = 3: the first digit of nums[1] is 5, and the last digit of nums[3] is 4. Indeed, gcd(5,4) == 1.
When i = 2 and j = 3: the first digit of nums[2] is 1, and the last digit of nums[3] is 4. Indeed, gcd(1,4) == 1.
Thus, we return 5.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [11,21,12]
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are 2 beautiful pairs:
When i = 0 and j = 1: the first digit of nums[0] is 1, and the last digit of nums[1] is 1. Indeed, gcd(1,1) == 1.
When i = 0 and j = 2: the first digit of nums[0] is 1, and the last digit of nums[2] is 2. Indeed, gcd(1,2) == 1.
Thus, we return 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 100</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 9999</code></li>
	<li><code>nums[i] % 10 != 0</code></li>
</ul>

from collections import Counter
from math import gcd
from typing import List

class Solution:
    def countBeautifulPairs(self, nums: List[int]) -> int:
        """
        Count the number of beautiful pairs in the list of integers.

        A pair of integers (num1, num2) is called beautiful if the greatest common divisor (GCD)
        of the first digit of num1 and the last digit of num2 is 1.

        Args:
        nums (List[int]): A list of positive integers.

        Returns:
        int: The count of beautiful pairs where the GCD of the first digit of one number and the last digit of another number is 1.
        """

        def getFirstDigit(num: int) -> int:
            """
            Extract the first digit of a positive integer.

            Args:
            num (int): A positive integer.

            Returns:
            int: The first digit of the integer.
            """
            while num >= 10:
                num //= 10
            return num

        def getLastDigit(num: int) -> int:
            """
            Extract the last digit of a positive integer.

            Args:
            num (int): A positive integer.

            Returns:
            int: The last digit of the integer.
            """
            return num % 10

        def isBeautifulPair(num1: int, num2: int) -> bool:
            """
            Check if a pair of integers is beautiful.

            A pair (num1, num2) is considered beautiful if the GCD of the first digit of num1 and the last digit of num2 is 1.

            Args:
            num1 (int): The first number in the pair.
            num2 (int): The second number in the pair.

            Returns:
            bool: True if the GCD of the first digit of num1 and the last digit of num2 is 1, False otherwise.
            """
            first_digit = getFirstDigit(num1)
            last_digit = getLastDigit(num2)
            return gcd(first_digit, last_digit) == 1

        # Initialize the count of beautiful pairs to 0
        beautiful_pairs_count = 0
        first_digits = [getFirstDigit(num) for num in nums]
        last_digits = [getLastDigit(num) for num in nums]

        # Iterate over all possible pairs (i, j) where i < j
        n = len(nums)
        for i in range(n-1):
            for j in range(i+1, n):
                if gcd(first_digits[i], last_digits[j]) == 1:
                    beautiful_pairs_count += 1  # Increment the count for each beautiful pair found

        return beautiful_pairs_count


        
147