Python Dictionary 29ms beats 90%


  • 0
    R
            if s is None:
                return False
            s_dict = {}
            for letter in s:
                if letter not in s_dict:
                    s_dict[letter] = 1
                else:
                    s_dict[letter] += 1
            
            seen_odd = False
            
            for value in s_dict.values():
                if value % 2 == 1:
                    if seen_odd == False:
                        seen_odd = True
                    else:
                        return False
            
            return True
    

    What we want to do is see if we have only one odd amount of letters in our word, so we add all letters to a dictionary and iterate through them at the end to see. If we have two odd amounts, we return False


Log in to reply
 

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