Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

Solution: Make The String Great

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.

Continue to do this until there are no more adjacent characters of the same letter but in different cases. An empty string is also considered "good".

Examples

Example 1

  • Input: "AaBCcdEeff"
  • Output: "Bdff"
  • Explanation: In the first step, "AaBCcDEeff" becomes "BcCDdEeff" because 'A' and 'a' are the same letter, but one is uppercase and the other is lowercase. Then we remove "cC", and "Ee"

.....

.....

.....

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

Akhmadullo Shokirov

· 5 months ago

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

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); } }

Reading Progress

0%