File: root - text - article - 2019 - 10 - daily-problem-validate-balanced-parentheses.txt
Tags: 每日算法, 括号匹配, 面试, Python, daily problem, Validate Balanced Parentheses, algorithms, twitter interview question, python, | English | Home Page | Category: Computing | 558 Views, 25637 Search Bots | 161 Words
| Browse | Archive
Tags: 每日算法, 括号匹配, 面试, Python, daily problem, Validate Balanced Parentheses, algorithms, twitter interview question, python, | English | Home Page | Category: Computing | 558 Views, 25637 Search Bots | 161 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by Uber:
Imagine you are building a compiler. Before running any code, the compiler must check that the parentheses in the program are balanced. Every opening bracket must have a corresponding closing bracket. We can approximate this using strings.
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Note that an empty string is also considered valid.
Example:
Input: "((()))"
Output: True
Input: "[()]{}"
Output: True
Input: "({[)]"
Output: False
Tags: 每日算法, 括号匹配, 面试, Python, daily problem, Validate Balanced Parentheses, algorithms, twitter interview question, python, | English | Home Page | Cateogry: Computing | 558 Views, 25637 Search Bots | 161 Words Imagine you are building a compiler. Before running any code, the compiler must check that the parentheses in the program are balanced. Every opening bracket must have a corresponding closing bracket. We can approximate this using strings.
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Note that an empty string is also considered valid.
Example:
Input: "((()))"
Output: True
Input: "[()]{}"
Output: True
Input: "({[)]"
Output: False
class Solution:
def isValid(self, s):
# Fill this in.
# Test Program
s = "()(){(())"
# should return False
print(Solution().isValid(s))
s = ""
# should return True
print(Solution().isValid(s))
s = "([{}])()"
# should return True
print(Solution().isValid(s))
Related Articles
- Daily Interview Problem: Group Words that are Anagrams
- Daily Interview Problem: Buddy Strings
- Daily Interview Question: Create a Simple Calculator
- Non-decreasing Array with Single Modification
- Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
- Making a Height Balanced Binary Search Tree
- Daily Interview Puzzle: Minimum Size Subarray Sum
- [Daily Problem] Move Zeros
- Daily Interview Question: Find Cycles in a Graph
- Daily Interview Problem: 3 Sum
©2006~2024 SteakOverCooked - 0.00729 Seconds(s) - 2785.989 KB/s - 12 Online Memory: 494.62 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !