Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You are given a string s, consisting of lowercase letters. You need to make duplicate removal on s until you no longer can.
- A duplicate removal consists of choosing two adjacent equal characters and removing both of them.
- Return the final string after all such duplicate removals have been made.
- Input format
- The first line contains a single string s.
- Output format
- Print the final string after duplicate removals.
- Function definition
- You have to complete the given function. It accepts one argument -the input string, and returns the updated string.
- Sample Input 1
- abbaca
- Sample Output 1
- ca
- Explanation
- Initial String: abbaca
- After removing "bb" : aaca
- After removing "aa" : ca (There are no more duplicates)
- Constraints
- 1 <= |s| <= 10^5
- */
- /**
- * @param {String} str
- * @return {String}
- */
- function removeAdjacentDuplicates(str) {
- let stack=[];
- let ans='';
- for(let i=0;i<str.length;i++){
- let curr=str.charAt(i);
- if(stack.length!=0&&stack[stack.length-1]==curr){
- stack.pop();
- }
- else{
- stack.push(curr);
- }
- }
- ans=stack.join('');
- return ans;
- }
- function main() {
- let s = readLine();
- let result = removeAdjacentDuplicates(s);
- console.log(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement