<p>You are given an integer array <code>nums</code>. You have to find the <strong>maximum</strong> sum of a pair of numbers from <code>nums</code> such that the <strong>largest digit </strong>in both numbers is equal.</p>

<p>For example, 2373 is made up of three distinct digits: 2, 3, and 7, where 7 is the largest among them.</p>

<p>Return the <strong>maximum</strong> sum or -1 if no such pair exists.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [112,131,411]</span></p>

<p><strong>Output:</strong> <span class="example-io">-1</span></p>

<p><strong>Explanation:</strong></p>

<p>Each numbers largest digit in order is [2,3,4].</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2536,1613,3366,162]</span></p>

<p><strong>Output:</strong> <span class="example-io">5902</span></p>

<p><strong>Explanation:</strong></p>

<p>All the numbers have 6 as their largest digit, so the answer is <span class="example-io">2536 + 3366 = 5902.</span></p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [51,71,17,24,42]</span></p>

<p><strong>Output:</strong> <span class="example-io">88</span></p>

<p><strong>Explanation:</strong></p>

<p>Each number&#39;s largest digit in order is [5,7,7,4,4].</p>

<p>So we have only two possible pairs, 71 + 17 = 88 and 24 + 42 = 66.</p>
</div>

<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;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def maxSum(self, nums: List[int]) -> int:
        maxims = []
        lista = []

        for number in nums:
            maxim = 0
            for a in str(number):
                if int(a) >= maxim:
                    maxim = int(a)
            maxims.append(maxim)

        diccionario_contar = {}

        for i in maxims:
            diccionario_contar[i] = maxims.count(i)


        lista = [] 

        
        valor = 0

        for key, value in diccionario_contar.items():
            if value != 1:
                lista.append(key)
                valor +=1
            
        if valor == 0:
            return -1
                


        dict_res = {}
        for i in lista:  # Aquí iteramos sobre 'i' en la lista que contiene los máximos de interés
            for number in nums:  # Para cada número en la lista nums
                maxim = 0  # Inicializamos el valor máximo en 0
                for a in str(number):  # Convertimos el número a cadena para poder iterar sobre cada
                    if int(a) >= maxim:  # Verificamos si el dígito actual es mayor o igual al máximo
                        maxim = int(a)  # Si es así, actualizamos el máximo
                if maxim == i:  # Si el máximo es igual al valor de 'i'
                    if maxim not in dict_res:  # Si el máximo no está ya en el diccionario, lo 
                        dict_res[maxim] = []
                    dict_res[maxim].append(number)  # Agregamos el número actual a la lista asociada

        print(dict_res)
        

        lista_res = []


        for key, value in dict_res.items():
            valor = 0
            for i in sorted(value, reverse = True)[:2]:
                valor += i
            lista_res.append(valor)
        
        resultado = max(lista_res)

        return resultado









247