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.
C++
Trick is to use a stack, but can just use a string the same way.
Moving from left to right, if the current char is the same as on the top of stack (or .back() of string) pop it (pop_back()), and do not push it to stack, effectivly deleting them.
Else we push character to the stack (push_back()).
If we use a string as a stack, we can just return this resulting string;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Super Reduced String
You are viewing a single comment's thread. Return to all comments →
C++ Trick is to use a stack, but can just use a string the same way. Moving from left to right, if the current char is the same as on the top of stack (or .back() of string) pop it (pop_back()), and do not push it to stack, effectivly deleting them. Else we push character to the stack (push_back()). If we use a string as a stack, we can just return this resulting string;