Answer to Question #267620 in C++ for zxc

Question #267620

Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest length by doing a series of operations. In each operation, he selects a triple of adjacent lowercase letters that match, and he deletes them. For instance, the string aaab could be shortened to b in one operation. Steve’s task is to delete as many characters as possible using this method and print the resulting string. If the final string is empty, print "Empty String" without quotes. Characters can be deleted only if they form a triple and are the same(i.e. from aaaa we can only delete 3 a's and will be left with a single a).


Example:

Sample Input: aaaabcccdd

Sample Output: abdd


1
Expert's answer
2021-11-17T17:35:15-0500
function superReducedString(s) {
    const values = [...s].reduce((target, item) => {
        if (target.slice(-1)[0] !== item) {
            return [...target, item];
        }

        target.pop(item);

        return target;
    }, []);

    return values.length > 0 ? values.join('') : 'Empty String';
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS