LeetCode Valid Parentheses

LeetCode Valid Parentheses

The JavaScript Way

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.

Example 1:

Input: s = "()"
Output: true

Example 2:

Input: s = "()[]{}"
Output: true

Example 3:

Input: s = "(]"
Output: false

Solution

explain this code:/**
* @param {string} s
* @return {boolean}
*/

class Stack {
constructor() {
this.count = 0;
this.items = {};
}
isEmpty() {
return this.count == 0;
}

push(element) {
this.items[this.count] = element;
this.count++;
}
pop() {
if (this.isEmpty()) {
return "";
}

this.count--;

const result = this.items[this.count];
delete this.items[this.count];
return result;
}
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.count - 1];
}
size() {
return this.count;
}
clear() {
this.items = {};
this.count = 0;
}
toString() {
if (this.isEmpty()) {
return "";
}
let objString = `${this.items[0]}`;
for (let i = 1; i < this.count; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}

This is our main stack:

The code defines a class called Stack that implements a stack data structure using an object. The Stack class has the following methods:

  • constructor(): initializes an empty object to store the stack and sets the count to zero.
  • isEmpty(): returns true if the stack is empty and false otherwise.
  • push(element): adds an element to the top of the stack and increments the count.
  • pop(): removes the top element from the stack and returns it. If the stack is empty, it returns an empty string.
  • peek(): returns the top element of the stack without removing it. If the stack is empty, it returns undefined.
  • size(): returns the number of elements in the stack.
  • clear(): removes all the elements from the stack and resets the count to zero.
  • toString(): returns a string representation of the stack.

Now lets look into the actual solution

var isValid = function(s) {
checkValidPair=(a)=>{
// console.log(first)

if(stack.peek()=="(" && a==")"){
return true
}
if(stack.peek()=="{" && a=="}"){
return true
}
if(stack.peek()=="[" && a=="]"){
return true
}
else return false
}

let n = s.length;
// console.log(n);
const stack = new Stack();
for (let i = 0; i <=n ; i++) {
if (s[i] == "(" || s[i] == "{" || s[i] == "[") {
stack.push(s[i]);
console.log(stack.peek())
} else if (s[i] == ")" || s[i] == "}" || s[i] == "]") {
if (stack.isEmpty() || !checkValidPair(s[i])) {

return false;
} else {
stack.pop();
}
}
}
// console.log("stack", stack.toString());
return stack.isEmpty() ? true : false;
};

The isValid function checks whether a given string s is balanced or not. It creates a new instance of the Stack class and iterates over the string using a for loop. For each character in the string, if it is an opening parentheses, bracket, or curly brace, it is pushed onto the stack. If it is a closing parentheses, bracket, or curly brace, it is checked against the top element of the stack to see if they form a valid pair. If they do, the top element is removed from the stack; otherwise, the function returns false. After the loop ends, if the stack is empty, it returns true; otherwise, it returns false.

I will be trying to solve all the other LeetCode problems. Follow Adarsh gupta for more.