Skip to content

Commit c84f03c

Browse files
authored
Merge pull request #43 from komalreddy3/valid_parantheses
Added a Algorithm named Valid parentheses
2 parents 4773a33 + a821377 commit c84f03c

File tree

2 files changed

+200
-1
lines changed

2 files changed

+200
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// INTRODUCTION
2+
3+
// Javascript implementation to check if string has valid number of parentheses
4+
5+
// THE CHALLENGE
6+
7+
// check whether the string has valid number of parentheses or not
8+
9+
// ALGORITHMIC THINKING
10+
11+
12+
// 1. Initialize an empty stack:
13+
// 1.1 Create an empty array called stack to serve as the stack.
14+
15+
// 2 Iterate through each character in the input string:
16+
// 2.1 Use a for...of loop to iterate through each character (ch) in the given string (s).
17+
18+
// 3. Check for opening brackets:
19+
// 3.1 If the current character is an opening bracket ('(', '{', '['), push it onto the stack.
20+
21+
// 4. Check for closing brackets:
22+
// 4.1 If the current character is a closing bracket (')', '}', ']'):
23+
// 4.1.1 Check if the stack is empty. If so, return false, as there is no matching opening bracket.
24+
// 4.1.2 Check if the top of the stack matches the corresponding opening bracket. If not, return false.
25+
// 4.1.3 If the brackets match, pop the top element from the stack.
26+
27+
// 5. Check for remaining elements in the stack:
28+
// 5.1 After processing all characters in the input string, check if there are any remaining elements in the stack.
29+
// 5.1.1 If the stack is empty, return true, indicating a valid string.
30+
// 5.1.2 If the stack is not empty, return false, indicating an invalid string.
31+
32+
33+
// CODE IMPLEMENTATION
34+
35+
function validString (s){
36+
let stack=[];
37+
for(let ch of s){
38+
if(ch=='(' || ch=='{' || ch=='[') stack.push(ch);
39+
else{
40+
if(!stack.length || (ch==')' && stack[stack.length-1]!=='(') || (ch==']' && stack[stack.length-1]!=='[') || (ch=='}' && stack[stack.length-1]!=='{')) return false;
41+
else stack.pop();
42+
}
43+
}
44+
if(stack.length==0) return true;
45+
else return false;
46+
}
47+
48+
if(validString("()]")){
49+
console.log("Its a valid string");
50+
}else{
51+
console.log("Its not a valid string");
52+
}

README.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,4 +1649,151 @@ __code Implementation:__ <p>
16491649
16501650
16511651
<hr>
1652-
<hr>
1652+
<hr>
1653+
1654+
<b>15. Valid Parentheses </b>
1655+
1656+
<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
1712+
}
1713+
```
1714+
1715+
Step 4: Check for closing brackets
1716+
1717+
```js
1718+
else {
1719+
// Step 4: Check for closing brackets
1720+
if (
1721+
!stack.length ||
1722+
(ch == ')' && stack[stack.length - 1] !== '(') ||
1723+
(ch == ']' && stack[stack.length - 1] !== '[') ||
1724+
(ch == '}' && stack[stack.length - 1] !== '{')
1725+
) {
1726+
return false; // Invalid string, mismatched brackets
1727+
} else {
1728+
stack.pop(); // Pop the matching opening bracket from the stack
1729+
}
1730+
}
1731+
```
1732+
1733+
Step 5: Check for remaining elements in the stack
1734+
1735+
1736+
```js
1737+
if (stack.length == 0) {
1738+
return true; // Valid string, all brackets matched
1739+
} else {
1740+
return false; // Invalid string, unmatched opening brackets
1741+
}
1742+
```
1743+
1744+
Step 6. Example Usage
1745+
1746+
```js
1747+
if (validString("()]")) {
1748+
console.log("It's a valid string");
1749+
} else {
1750+
console.log("It's not a valid string");
1751+
}
1752+
```
1753+
Let's Implement
1754+
1755+
```js
1756+
function validString(s) {
1757+
// Step 1: Initialize an empty stack
1758+
let stack = [];
1759+
1760+
// Step 2: Iterate through each character in the input string
1761+
for (let ch of s) {
1762+
// Step 3: Check for opening brackets
1763+
if (ch == '(' || ch == '{' || ch == '[') {
1764+
stack.push(ch); // Push opening bracket onto the stack
1765+
} else {
1766+
// Step 4: Check for closing brackets
1767+
if (
1768+
!stack.length ||
1769+
(ch == ')' && stack[stack.length - 1] !== '(') ||
1770+
(ch == ']' && stack[stack.length - 1] !== '[') ||
1771+
(ch == '}' && stack[stack.length - 1] !== '{')
1772+
) {
1773+
return false; // Invalid string, mismatched brackets
1774+
} else {
1775+
stack.pop(); // Pop the matching opening bracket from the stack
1776+
}
1777+
}
1778+
}
1779+
1780+
// Step 5: Check for remaining elements in the stack
1781+
if (stack.length == 0) {
1782+
return true; // Valid string, all brackets matched
1783+
} else {
1784+
return false; // Invalid string, unmatched opening brackets
1785+
}
1786+
}
1787+
1788+
// Step 6: Example usage
1789+
if (validString("()]")) {
1790+
console.log("It's a valid string");
1791+
} else {
1792+
console.log("It's not a valid string");
1793+
}
1794+
```
1795+
</p>
1796+
1797+
1798+
<hr>
1799+
<hr>

0 commit comments

Comments
 (0)