Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

​

Optional Practice: Make The String Great (easy)

This problem is optional practice. It is the same idea as Balanced Parentheses and Remove All Adjacent Duplicates earlier in the chapter: scan the string once, and pop the stack when the top cancels with what you are looking at. Work through it for a third repetition of that pattern, or skip it and carry on.

Problem Statement

Given a string of English lowercase and uppercase letters, make the string "good" by removing two adjacent characters that are the same but in different cases.

.....

.....

.....

Like the course? Get enrolled and start learning!
Akhmadullo Shokirov

Akhmadullo Shokirov

· 7 months ago

In the solution, reversing operation is missing before it’s being returned

Show 1 reply
G

gabbygabbylexy

· a year ago

using System; using System.Collections.Generic; using System.Text; public class Solution { public string makeGood(string s) { // ToDo: Write Your Code Here. var stack = new Stack<char>(); foreach(var c in s) { if(stack.Count > 0 && char.ToLower(stack.Peek()) == char.ToLower(c) && stack.Peek() != c) { stack.Pop(); } else { stack.Push(c); } } var reverse = new Stack<char>(); while (stack.Count > 0) { reverse.Push(stack.Pop()); } return string.Join("", reverse); } }
Show 1 reply

Reading Progress

0%


Vote for new content