0% found this document useful (0 votes)
5 views1 page

Given A String S Containing Just TH

The document describes a function that checks if a string of brackets is valid based on matching pairs. It uses a stack to track opening brackets and ensures that each closing bracket corresponds to the correct opening bracket. The function returns true if the string is valid and false otherwise.

Uploaded by

sachin verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Given A String S Containing Just TH

The document describes a function that checks if a string of brackets is valid based on matching pairs. It uses a stack to track opening brackets and ensures that each closing bracket corresponds to the correct opening bracket. The function returns true if the string is valid and false otherwise.

Uploaded by

sachin verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',

determine if the input string is valid.


//example 1: "()[]{}"

//example 2: "([])"

//example 3 : "[(])"

function checkString(input) {
const stack = [];
const brackets = {
'(' : ')',
'[' : ']',
'{' : '}',
};
for(let i of input) {
if([')', ']', '}'].includes(i)) {
stack.push(i);
} else if(['(', '[', '{'].includes(i)) {
const ele = stack.pop();
if(brackets[i] !== ele) {
return false;
}
}
}
return stack.length === 0;
}

let str1 = ")(][}{";


console.log(checkString(str1));

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy