Given a string as alphanumeric characters where some characters repeat in consecutive places, we have to scatter repeated characters to make them non-consecutive.
input: aaaabbbcdd11
Output: abababacd1d1 or any other valid combination.
If there is no such combination, then there should be no output.
This question was asked me in the interview. I am still unable to solve this algorithm. I am not sure whether this is right forum to ask or not. but I am very curios to know the right algorithm/data structure that can be used to solve this.
I tried to create a hash (map) to so that I can keep the count of all characters.
str = "aaaabbbcdd11"
hashChrs = {}
for item in list(str):
if item in hashChrs:
hashChrs[item] = hashChrs[item] + 1
else:
hashChrs[item] = 1
print hashChrs
{'a': 4, '1': 2, 'c': 1, 'b': 3, 'd': 2}
When the highest frequency characters count will be more than half of total characters then no output will be generated.
Now I am not able to print all the desired output using map