Super Reduced String

Super Reduced String

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 pair of adjacent lowercase letters that match, and he deletes them. For instance, the string aab 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

Function Description

Complete the superReducedString function in the editor below. It should return the super reduced string or Empty String if the final string is empty.

superReducedString has the following parameter(s):

  • s: a string to reduce

Input Format

A single string, s.

Constraints

  • 1 <= |s| <= 100

Output Format

If the final string is empty, print Empty String; otherwise, print the final non-reducible string.

Sample Input 0

1
aaabccddd

Sample Output 0

1
abd

Explanation 0

Steve performs the following sequence of operations to get the final string:

1
aaabccddd → abccddd → abddd → abd

Sample Output 1

1
Empty String

Explanation 1

1
aa → Empty String

Sample Input 2

1
baab

Sample Output 2

1
Empty String

Explanation 2

1
baab → bb → Empty String

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Complete the superReducedString function below.
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';
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×