Created
October 7, 2019 10:22
-
-
Save PH9/4ade87bbf7aab0365957828a894d4b87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stack<T> { | |
private(set) var elements: [T] = [] | |
func push(_ e: T) { | |
elements.append(e) | |
} | |
func pop() -> T? { | |
defer { | |
elements.removeLast() | |
} | |
return elements.last | |
} | |
func peek() -> T? { | |
return elements.last | |
} | |
} | |
func removeAllPairs(_ input: String) -> String { | |
var stack = Stack<Character>() | |
for c in input { | |
if stack.peek() == c { | |
_ = stack.pop() | |
} else { | |
stack.push(c) | |
} | |
} | |
return stack.elements.map { String($0) }.joined() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment