Can someone review if this code is valid as a solution? - The difference is that I’m not popping and I’m using 2 different counters. The time complexity is still linear O(n), right? + I’m still using stack methods. So approved or rejected? Thanks~
var testString1 = "h((e))llo(world)()"
func checkParentheses(_ string: String) -> Bool {
// counter 1 for (
var count1 = 0
// counter 2 for )
var count2 = 0
// instantiate the stack
var stack = Stack<Character>()
// load the stack
for value in string {
if value == "(" {
count1 += 1
} else if value == ")" {
count2 += 1
}
stack.push(value)
}
// check for empty
if stack.isEmpty {
print("Stack contains 0 elements")
return false
}
//print both counters just out of curiosity
print(count1)
print(count2)
// Ternary operator
return count1 == count2 ? true : false
// check for success
// check for failure
}
checkParentheses(testString1) // should be true