We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defalternatingCharacters(s):# Write your code herechar_map={}last_letter_seen=Noneiflen(s)<=1:return0foreach_charins:ifeach_charnotinchar_map:char_map[each_char]=[1]else:iflast_letter_seenisnotNone:iflast_letter_seen==each_char:char_map[each_char][-1]+=1else:char_map[each_char].append(1)last_letter_seen=each_charmin_deletions=0forchar,occinchar_map.items():min_deletions+=sum(occ)-len(occ)returnmin_deletions
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Alternating Characters
You are viewing a single comment's thread. Return to all comments →
a brute force solution in python3