
g0001_0100.s0020_valid_parentheses.solution.go Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
package s0020_valid_parentheses
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
// #2024_03_07_Time_1_ms_(74.95%)_Space_2_MB_(68.19%)
func isValid(s string) bool {
stack := make([]rune, 0)
for _, c := range s {
switch c {
case '(', '[', '{':
stack = append(stack, c)
case ')':
if len(stack) == 0 || stack[len(stack)-1] != '(' {
return false
}
stack = stack[:len(stack)-1]
case '}':
if len(stack) == 0 || stack[len(stack)-1] != '{' {
return false
}
stack = stack[:len(stack)-1]
case ']':
if len(stack) == 0 || stack[len(stack)-1] != '[' {
return false
}
stack = stack[:len(stack)-1]
}
}
return len(stack) == 0
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy