Beautiful Binary String

Solutions The problem can be solved with a greedy algorithm: replace all 010 with 011. Anytime a replace is performed, a counter is incremented: 1 2 3 4 5 6 7 8 n = input() B = list(raw_input()) cnt = 0 for i in xrange(n): if B[i: i + 3] == ['0', '1', '0']: B[i + 2] = '1' cnt += 1 print cnt The code above works in-place but it does modify the input string. [Read More]

Making Anagrams

Solutions Two strings are anagrams of one another if they share the same characters and each character has the same frequency in both strings. Thus, we can easily solve this problem with a frequency table. Basically, we can add 1 for each character in a and subtract 1 for each character in b. Those characters with non-zero frequency must be deleted and then added to the total count. Here is an implementation in C++: [Read More]