[Python] I don't understand why my hashmap is not getting sorted

Discussão em 'Python' iniciado por Stack, Setembro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently doing the daily problem on leetcode and I can't understand why my hashmaps are not getting sorted, for "successes" I got as sorted hashmaps: {u'u': 1, u'e': 2} and {u's': 4, u'c': 2}. My code is the following:

    class Solution(object):
    def maxFreqSum(self, s):
    """
    :type s: str
    :rtype: int
    """
    vowel_count = 0
    consonant_count = 0
    vowels = set("AEIOUaeiou")
    vowel_dict = {}
    consonant_dict = {}

    for i in s:
    if i in vowels:
    vowel_dict = vowel_dict.get(i, 0) + 1
    else:
    consonant_dict = consonant_dict.get(i, 0) + 1

    vowel_dict = {k: v for k, v in sorted(vowel_dict.items(), key=lambda item: item[1], reverse=True)}
    consonant_dict = {k: v for k, v in sorted(consonant_dict.items(), key=lambda item: item[1], reverse=True)}

    print(vowel_dict)
    print(consonant_dict)

    if vowel_dict:
    vowel_count = next(iter(vowel_dict.values()))
    if consonant_dict:
    consonant_count = next(iter(consonant_dict.values()))
    return vowel_count + consonant_count


    I don't understand why one hashmap is sorted and the other one no.

    Continue reading...

Compartilhe esta Página