Python code check


  • 0
    S

    I was wondering if anyone can tell me what is wrong with my code :/

    Getting it wrong on test case: "civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"

    Where the expected is 983 but im getting 655

    '''

        odd_max = 0
        s_map = {}
        result = 0
    
        for i in range(len(s)):
            if s[i] not in s_map:
                s_map[s[i]] = 1
            else:
                s_map[s[i]] += 1
                
        for i in s_map:
            if s_map[i]%2 == 0:
                result += s_map[i]
            else:
                if odd_max < s_map[i]:
                    odd_max = s_map[i]
        return result+odd_max

  • 1
    H

    Not sure whether you figured it out or not. But I made the same mistake as you did at first.
    The reason you get 655 is because you ruled out all the letters that have odd count. Actually, you can still use these odd count letters, and just use count - 1 from them. For example, you get 27 'a', and you don't have to throw out all of them. Instead, you can still use 26 'a's.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.