Skip to content

Instantly share code, notes, and snippets.

@samarthsewlani
Created February 21, 2021 09:07
Show Gist options
  • Save samarthsewlani/e57f2023a13642e45b313c97a90a4357 to your computer and use it in GitHub Desktop.
Save samarthsewlani/e57f2023a13642e45b313c97a90a4357 to your computer and use it in GitHub Desktop.
Super Reduced String HackerRank Solution ( Efficient )
def superReducedString(s):
stack=["#"] #Initializing the stack with special character because if there are no elements and we access the top of stack it results into IndexError
for i in s:
if stack[-1]==i: #If the element is same as top of stack
stack.pop() #Pop the last element and do not push current element
else:
stack.append(i) #Else push the element in the stack
ans=""
for i in stack[1:]: #Ignoring the first index
ans+=i #Store the answer in the variable 'ans'
if ans:
return ans #If string is not empty return it
return "Empty String" #Else return "Empty String"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment