Remove redundant characters in a given string.

Write a function to remove all redundant characters in a given string
with minimum space and time complexity