Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created February 5, 2020 18:32
Show Gist options
  • Save les-peters/c58d3f82056a167b2b1303d07081d020 to your computer and use it in GitHub Desktop.
Save les-peters/c58d3f82056a167b2b1303d07081d020 to your computer and use it in GitHub Desktop.
IQofW 2020-02-04
# Given that an "even word" is a word in which each character appears an even
# number of times, write a function that takes in a string and returns the minimum
# number of letters to be removed to make that string an even word.
# Example:
# evenWord('aaaa')
# > 0
# evenWord('potato')
# > 2
def evenWord(word):
letterCounts = {}
oddLetterCounts = 0
for letter in word:
if letter in letterCounts:
letterCounts[letter] += 1
else:
letterCounts[letter] = 1
for letter in letterCounts.keys():
if letterCounts[letter] % 2 == 1:
oddLetterCounts += 1
return oddLetterCounts
print(evenWord('aaaa'))
print(evenWord('potato'))
print(evenWord('mississippi'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment