You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p> Valid parentheses refer to a sequence of parentheses – including round brackets (), square brackets [], and curly braces {} – that is properly nested and closed. In a valid expression, each opening parenthesis has a corresponding closing parenthesis, and they are arranged in a balanced manner.</p>
1657
+
1658
+
<p> Valid parentheses play a crucial role in programming, especially in expressions and syntax. Properly balanced parentheses are essential to ensure that code is interpreted correctly and avoids errors.</p>
1659
+
1660
+
__The challenge:__ <p> - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
1661
+
1662
+
An input string is valid if:
1663
+
1664
+
Open brackets must be closed by the same type of brackets.
1665
+
Open brackets must be closed in the correct order.
1666
+
Every close bracket has a corresponding open bracket of the same type.</p>
1667
+
1668
+
```js
1669
+
s="()"
1670
+
output = Its a valid string
1671
+
1672
+
s="()]"
1673
+
output= Its not a valid string
1674
+
```
1675
+
1676
+
__Algorithmic Thinking:__ <p>1. Initialize an empty stack:
1677
+
1.1 Create an empty array called stack to serve as the stack.
1678
+
1679
+
2 Iterate through each character in the input string:
1680
+
2.1 Use a for...of loop to iterate through each character (ch) in the given string (s).
1681
+
1682
+
3. Check for opening brackets:
1683
+
3.1 If the current character is an opening bracket ('(', '{', '['), push it onto the stack.
1684
+
1685
+
4. Check for closing brackets:
1686
+
4.1 If the current character is a closing bracket (')', '}', ']'):
1687
+
4.1.1 Check if the stack is empty. If so, return false, as there is no matching opening bracket.
1688
+
4.1.2 Check if the top of the stack matches the corresponding opening bracket. If not, return false.
1689
+
4.1.3 If the brackets match, pop the top element from the stack.
1690
+
1691
+
5. Check for remaining elements in the stack:
1692
+
5.1 After processing all characters in the input string, check if there are any remaining elements in the stack.
1693
+
5.1.1 If the stack is empty, return true, indicating a valid string.
1694
+
5.1.2 If the stack is not empty, return false, indicating an invalid string.
1695
+
</p>
1696
+
1697
+
1698
+
__code Implementation:__ <p>
1699
+
Step 1: Initialize an empty stack
1700
+
1701
+
```js
1702
+
let stack = [];
1703
+
```
1704
+
1705
+
Step 2: Iterate through each character in the input string
1706
+
1707
+
Step 3: Check for opening brackets
1708
+
1709
+
```js
1710
+
if (ch =='('|| ch =='{'|| ch =='[') {
1711
+
stack.push(ch); // Push opening bracket onto the stack
0 commit comments