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
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';
}
Comments
Leave a comment