Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
The problem Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets are closed by ...
Question
python
#3: Post edited
Balanced parentheses problem in Python
- Detecting balanced parentheses in Python
- # The problem
- > 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.- # The solution
- ```python
- def isValid(s: str) -> bool:
- if len(s) % 2 != 0:
- return False
- for i in range(len(s) // 2):
- s = s.replace("()", "").replace("[]", "").replace("{}", "")
- return len(s) == 0
- ```
- My approach to the problem is replacing the pairs. The string is balanced if the string is empty after replacing `len(str) // 2` times. Is this a good approach? How can I improve my algorithm?
- # The problem
- > 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 are closed by the same type of brackets.
- > 2. Open brackets are closed in the correct order.
- # The solution
- ```python
- def isValid(s: str) -> bool:
- if len(s) % 2 != 0:
- return False
- for i in range(len(s) // 2):
- s = s.replace("()", "").replace("[]", "").replace("{}", "")
- return len(s) == 0
- ```
- My approach to the problem is replacing the pairs. The string is balanced if the string is empty after replacing `len(str) // 2` times. Is this a good approach? How can I improve my algorithm?
#2: Post edited
Balanced parentheses problem in Python
- # The problem
- > 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.
- # The solution
```- def isValid(s: str) -> bool:
- if len(s) % 2 != 0:
- return False
- for i in range(len(s) // 2):
- s = s.replace("()", "").replace("[]", "").replace("{}", "")
- return len(s) == 0
- ```
- My approach to the problem is replacing the pairs. The string is balanced if the string is empty after replacing `len(str) // 2` times. Is this a good approach? How can I improve my algorithm?
- # The problem
- > 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.
- # The solution
- ```python
- def isValid(s: str) -> bool:
- if len(s) % 2 != 0:
- return False
- for i in range(len(s) // 2):
- s = s.replace("()", "").replace("[]", "").replace("{}", "")
- return len(s) == 0
- ```
- My approach to the problem is replacing the pairs. The string is balanced if the string is empty after replacing `len(str) // 2` times. Is this a good approach? How can I improve my algorithm?
#1: Initial revision
Balanced parentheses problem in Python
# The problem > 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. # The solution ``` def isValid(s: str) -> bool: if len(s) % 2 != 0: return False for i in range(len(s) // 2): s = s.replace("()", "").replace("[]", "").replace("{}", "") return len(s) == 0 ``` My approach to the problem is replacing the pairs. The string is balanced if the string is empty after replacing `len(str) // 2` times. Is this a good approach? How can I improve my algorithm?