Coding Challenge from Leetcode - Valid Parentheses!
Автор: KG.codes
Загружено: 2026-02-01
Просмотров: 9
Описание:
Here is the problem and solution to a coding challenge from Leetcode.com called Valid Parentheses!
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.
Examples:
Input: s = "([])" Output: true
Input: s = "([)]" Output: false
Intuition / tip:
Use a stack to track opening brackets and ensure each closing bracket matches the most recent opening one.
Solution steps:
Create an empty stack.
Loop through each character in the string.
If the character is an opening bracket (, {, or [, push it onto the stack.
If it is a closing bracket:
Check the top element of the stack.
If it matches the correct opening bracket, pop it.
Otherwise, return false.
After looping, return true only if the stack is empty.
Time complexity:
O(n), since we loop through the string once.
Space complexity:
O(n), since in the worst case, all characters are stored in the stack.
Let me know if you have any questions or see ways I could have optimized!
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: