Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 17 additions & 41 deletions swift/0020-valid-parentheses.swift
Original file line number Diff line number Diff line change
@@ -1,47 +1,23 @@
class Solution {
// Input: s = "()[]{}"
func isValid(_ s: String) -> Bool {
let parenMap:[Character:Character] = ["(":")", "[":"]", "{":"}"]
var stack = Stack<Character>()
for c in s {
if parenMap.keys.contains(c) {
stack.push(c)
} else if stack.isEmpty || c != parenMap[stack.pop()!] {
return false
let closeToOpenDic: [Character:Character] = [
"}": "{",
")": "(",
"]": "["]
var stack = [Character]()
for char in s {
for char in s {
Comment on lines +9 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate of for-loop here

let c = closeToOpenDic[char]
if c == nil {
stack.append(char)
}else{
if stack.popLast() != c {
return false
}
}
}
}
return stack.isEmpty
}
}

class Node<T> {
var data: T
var next: Node<T>?
init(_ value: T) {
self.data = value
}
}

class Stack<T> {

var head: Node<T>?

var isEmpty: Bool {
head == nil
}

func peak() -> Node<T>? {
head
}

func push(_ data: T) {
var node = Node<T>(data)
node.next = head
head = node
}

func pop() -> T? {
var data = head?.data
head = head?.next
return data
return stack.count == 0
}
}