Valid Parentheses leetcode problem using JavaScript - javascript

I'm trying to figure out valid parentheses problem from leetcode using JavaScript and I couldn't figure out a plan on how to solve this 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 must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
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
My current thinking process is like this:
Split the string into an array (example: "{}" --> ["{","}", "[", "]", "(", ")"]
Loop through the array
Use the index of each characters to compare...?
Not sure after this...
Help please.

Here's a simple stack implementation:
const BRACKETS = [['(', ')'], ['[', ']'], ['{', '}']];
const OPEN = BRACKETS.reduce((a, [o, c]) => ({...a, [o]: c}), {});
const CLOSE = BRACKETS.reduce((a, [o, c]) => ({...a, [c]: o}), {});
const isBalanced = (s) => {
const stack = [];
for (const c of [...s]) {
if (c in OPEN) stack.push(c);
if (c in CLOSE && stack.pop() !== CLOSE[c]) return false;
}
return !stack.length;
};
console.log(isBalanced('{{[()]()}}'));
console.log(isBalanced('{[)}'));
I first create two lookup objects for opening and closing brackets. Then it's just a matter of looping over the characters, and:
if it's an opening bracket, push it onto the stack;
if it's a closing bracket, pop the last value off the stack and check whether it matches the closing bracket;
after everything is processed, check that the stack is empty.

const OPENING_BRACKETS = ['(', '[', '{']
const CLOSING_BRACKETS = [')', ']', '}']
const hasBalancedBrackets = text => !countUnmatchedBrackets(text)
function countUnmatchedBrackets(text) {
return [...text].filter(isBracket).reduce((stack, bracket) =>
isOpen(bracket) || !isMatch(stack.at(-1), bracket)
? /* push */ stack.concat(bracket)
: /* pop */ stack.slice(0, stack.length - 1), []).length
}
function isMatch(lastBracket, bracket) {
return OPENING_BRACKETS.some((openBracket, i) =>
lastBracket === openBracket &&
bracket === CLOSING_BRACKETS[i])
}
function isBracket(char) { return isOpen(char) || CLOSING_BRACKETS.includes(char) }
function isOpen(bracket) { return OPENING_BRACKETS.includes(bracket) }
[
[false, '[){'],
[true, 'a()a'],
[true, '{{[([a]a)a]a}}'],
[false, 'aa{a}a[aa(a](({[{a[[[()(([[a']
].forEach(([expected, input]) =>
console.assert(expected === hasBalancedBrackets(input), input))

Valid Parentheses In Javascript (Asked in many interviews. Simple and accepted solutions.)
Methods used here are string iteration and charAt and replace methods.
var isValid = function(s) {
let par ={'(' :')','{': '}', '[':']'};
for(let i=0;i<s.length;++i) {
if(s.charAt(i+1) && s.charAt(i+1)== par[s.charAt(i)]){
s = s.replace(s.charAt(i)+s.charAt(i+1),'');
i=i-2;
}
}
if(s.length){
console.log(false)
return false;
} else {
console.log(true)
return true;
}
};
console.log(isValid("()[]{}"))

const isValid = (s) => {
let stack = [];
const map = new Map([
['(', ')'],
['{', '}'],
['[', ']'],
]);
for (let i = 0; i < s.length; i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
stack.push(s[i]);
} else {
const last = stack.pop();
if (s[i] !== map.get(last)) {
return false;
}
}
}
return stack.length === 0;
};

Related

How to check for valid braces in javascript, programming problem?

have been struggling for the last couple of days with the following problem from codewars:
Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return  true  if the string is valid, and  false  if it's invalid.
All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces:  ()[]{} .
What is considered Valid?
A string of braces is considered valid if all braces are matched with the correct brace.
Examples
"(){}[]" => True
"([{}])" => True
"(}" => False
"[(])" => False
"[({})](]" => False
So I'm really stuck with the code for this one, and this is what I have up to this point:
function validBraces(braces){
let opening = [ '(', '[', '{']
let closing = [ ')', ']', '}']
let count = 0
const left = []
const right = []
// I generate left and right arrays, left w/the opening braces from the input, right w/ the closing
for (let i = 0; i < braces.length; i++) {
if (opening.includes(braces[i])) {
left.push(braces[i])
} else if (closing.includes(braces[i])) {
right.push(braces[i])
}
}
if (braces.length % 2 !== 0) {
return false
}
// I know there's no point in doing this but at one point I thought I was finishing the program and thought I would 'optimize it' to exit early, probably this is dumb haha.
if (left.length !== right.length) {
return false
}
// The juicy (not juicy) part where I check if the braces make sense
for (let i = 0; i < left.length; i++) {
// If the list are made up of braces like ()[]{} add one to counter
if (opening.indexOf(left[i]) === closing.indexOf(right[i])) {
count += 1
} else // If left and right are mirrored add one to the counter
if (opening.indexOf(left[i]) === closing.indexOf(right.reverse()[i])) {
count += 1
}
}
//If the counter makes sense return true
if (count === braces.length / 2) {
return true
} else { return false}
}
console.log(validBraces( "()" )) //true
console.log(validBraces("([])")) //true
console.log(validBraces( "[(])" )) //false
console.log(validBraces( "[(})" )) //false
console.log(validBraces( "[([[]])]" )) //true
Some comments: I know I'm still not checking for this example ([])() but I thought of breaking this up into two smaller checks in some way.
Thank you if you read up to this point. I would appreciate guidance in some way, though I don't want the problem solved for me. I'm probably overcomplicating this in some way since its a 6kyu problem, if so a tip on how to approach it more cleverly would be very much appreciated.
Thank you in advance! :pray: :pray:
Hell yeah!! I'm very happy to finally reach to the solution myself using some of the hints given to me here:
function validBraces(braces){
let opening = [ '(', '[', '{']
let closing = [ ')', ']', '}']
let arr = []
//console.log(closing.indexOf(braces[")"]) === opening.indexOf(arr[")"]))
for (let i = 0; i < braces.length; i++) {
if (opening.includes(braces[i])) {
arr.push(braces[i])
} else
if (closing.indexOf(braces[i]) === opening.indexOf(arr[arr.length - 1])) {
arr.pop()
} else return false
} return arr.length === 0;
}
I was clearly overthinking it in the first place haha. Thanks for everyone that helped!
As Dave suggested, using a stack, I've wrote the code for it:
var leftBraces="([{";
var rightBraces=")]}";
function checkBraces(braces) {
var ok=true;
var stack=[];
for(var i=0; i<braces.length && ok; i++) {
var brace=braces[i];
if(leftBraces.includes(brace)) stack.push(brace);
else {
var leftBrace=stack.pop();
if(leftBrace==undefined) ok=false;
else if(leftBraces.indexOf(leftBrace)!=rightBraces.indexOf(brace)) ok=false;
}
}
if(stack.length) ok=false;
return ok;
}
Code assumes only braces (no spaces or other characters).
I'm using string.indexOf() that matches for leftBraces and rightBraces.
Also, within the for loop, notice the termination part (2nd): i<braces.length && ok - doesn't "have to" use the iterator and, if I'm not mistaken, can even be empty...
var validBraces = (s) => {
let objO = {'(': 0, '[': 1, '{': 2};
let objC = {')': 0, ']': 1, '}': 2};
let stack = [];
for (let i=0; i<s.length; i++) {
if (objO.hasOwnProperty(s[i])) {
if (stack.length === 0 || stack[stack.length-1].idx!==objO[s[i]])
stack.push({idx: objO[s[i]], count: 1});
else
stack[stack.length-1].count++;
}
else if (objC.hasOwnProperty(s[i])) {
if (stack.length === 0 || stack[stack.length-1].idx!==objC[s[i]])
return false;
else {
stack[stack.length-1].count--;
if (stack[stack.length-1].count===0)
stack.pop();
}
}
}
return stack.length === 0;
};
console.log(validBraces("(){}[]"));
console.log(validBraces("([{}])"));
console.log(validBraces("(})"));
console.log(validBraces("[(])"));
console.log(validBraces("[({})](]"));
Here is a simplified solution:
let isMatchingBraces = function(str) {
let stack = [];
let symbol = {
'(': ')',
'[': ']',
'{': '}'
};
for (let i = 0; i < str.length; i += 1) {
// If character is an opening brace add it to a stack
if (str[i] === '(' || str[i] === '{' || str[i] === '[') {
stack.push(str[i]);
}
// If that character is a closing brace, pop from the stack, which will also reduce the length of the stack each time a closing bracket is encountered.
else {
let last = stack.pop();
//If the popped element from the stack, which is the last opening brace doesn’t match the corresponding closing brace in the symbol, then return false
if (str[i] !== symbol[last]) {
return false
};
}
}
// After checking all the brackets of the str, at the end, the stack is not
// empty then fail
if (stack.length !== 0) {
return false
};
return true;
}
function validBraces(braces){
let par =0;
let bra =0;
let cur =0;
for(let i =0; i<braces.length; i++){
if(braces[i]==="("){
par++;
}
if(braces[i]===")"){
par--;
}
if(braces[i]==="["){
bra++;
}
if(braces[i]==="]"){
bra--;
}
if(braces[i]==="{"){
cur++;
}
if(braces[i]==="}"){
cur--;
}
}
if(par<0 || bra<0 || cur<0){
return false;
}
return true;
};
Here is my solution:
var isValid = function (s) {
let charMap = new Map();
for (let i = 0; i < s.length; i++) {
charMap.set(s[i], i);
}
return Boolean(
charMap.get("(") < charMap.get(")") &&
charMap.get("(") % 2 != charMap.get(")") % 2 &&
charMap.get("{") < charMap.get("}") &&
charMap.get("{") % 2 != charMap.get("}") % 2 &&
charMap.get("[") < charMap.get("]") &&
charMap.get("[") % 2 != charMap.get("]") % 2
);
};
Explanation:
In order to achieve a quick and short solution, I have identified the common pattern of validity for opening/closing braces.
The common pattern for opening and closing braces' validity is that if say the opening(closing) stands at the even index in the string, the other one should be odd and vice versa. Example {}, {[]}, {()[]}, {[()]}.
Because we want to avoid a double loop for performance reasons, we are using a Hash Table via Map() to store the character and the index.
An alternative for getting the character's index would be using Array's find or another method, but that would end up in a second loop over the values which we want to avoid.
Finally, once the indexes of and the characters are stored in the charMap, we check whether or not the stored closing/opening characters' standing (odd/even) in the string is not equal, e.g. if '(' is odd the ')' should be even and vice versa.
We check this via the remainder (%) operator, i.e. a number's remainder of 2 is 0 if even.
Additionally, we need to check whether the order of braces is correct, e.g. if '(' is before '}';
The Boolean() function coerces the comparisons in the desired result.

How to write a function that recognises unbalanced brackets

I am trying to write a function that checks if a syntax is correct or not. If it is correct it returns 'ok' else it returns the index of the error. So far my code works if it is correct, if the error is at the first index or last index. Finding errors inbetween is what i am finding difficult. Here is my code.
function syntaxError(syntax) {
let arr = syntax.split('').join()
let arr1 = [];
let arr2 = [];
let result;
//Error if the first index contain a closing braces
if (arr[0] === '>' || arr[0] === ']' || arr[0] === '}' || arr[0] === ')') {
result = 0
};
if (arr === "") {
result = 'ok'
};
//Error if its just a single brace
if (arr.length === 1) {
result = 0
};
//Error if the last index contain an opening braces
if (arr.slice(-1) === '<' || arr.slice(-1) === '[' || arr.slice(-1) === '{' || arr.slice(-1) === '(') {
result = indexOf(arr.slice(-1))
};
let char = arr[i];
if (char == '[' || char == '{' || char == '<' || char == '(') {
arr1.push(char)
} else {
arr2.push(char);
}
if (arr1.length === 0 || arr2.length === 0) {
result = 0
}
if (arr1.length === arr2.length) {
result = 'ok'
}
return result
}
The example below should return 95
('[[[[[[[[[[[[[[]]]]]]]]<<<<<<<<<<<>>>>>>>>>>>]]]]]]'+'[[[[[[[[[[[[[[]]]]]]]
<<<<<<<<<<<>>>>>>>>>>>]}]]]]' + '>')
You could take a an array for the index of each opening character and pop this if the related closing character is found.
If finished and the stack has no item, the syntax is ok, otherwise return an index or the index of the last pushed opening character.
Example:
code comment
----------- ---------------------------
[][[[]][][]
[] balanced
[ error, this returns later 2
[[]] balanced
[] balanced
[] balanced
finally a missing ]
function syntaxError(syntax) {
const
isOpening = c => /[<[{(]/.test(c),
isClosing = c => /[>\]})]/.test(c),
open = { '>': '<', ']': '[', '}': '{', ')': '(' };
var stack = [],
index,
finished = Array
.from(syntax)
.every((c, i) => {
var temp = stack[stack.length - 1];
if (isOpening(c)) {
if (temp && temp.c === c) {
temp.indices.push(i);
} else {
stack.push({ c, indices: [i] });
}
return true;
}
if (isClosing(c)) {
if (temp && temp.c === open[c]) {
temp.indices.pop();
if (!temp.indices.length) stack.pop();
} else {
index = stack.length ? stack.pop().indices.pop() : i;
return false;
}
}
return true;
});
return finished && !stack.length
? 'ok'
: index === undefined
? stack.pop().indices.pop()
: index;
}
console.log(syntaxError('[][][[{}]]')); // ok
console.log(syntaxError(')'));
// 0
console.log(syntaxError('[][][[{<}]]'));
// 01234567
console.log(syntaxError('[][[[]][][]'));
// 012
console.log(syntaxError('[[[[[[[[[[[[[[]]]]]]]]<<<<<<<<<<<>>>>>>>>>>>]]]]]]'+'[[[[[[[[[[[[[[]]]]]]]<<<<<<<<<<<>>>>>>>>>>>]}]]]]' + '>'));
An algorithm for doing this is, find an opening symbol, push it to a stack (or array, whatever). Find another opening symbol, push it to a stack. Find a closing symbol that matches the top of the stack, pop the opening symbol off the stack. Find a closing symbol that doesn't match the top of the stack, you found an error. Find a closing symbol and there's nothing on the stack, you found an error. Get to the end and still have symbols on the stack, you found an error.

Write a function which takes in a string and returns the counts of each string [duplicate]

This question already has answers here:
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
Closed 3 years ago.
I'm literally stuck at solving this simple question. Anyway I found out another way to solve this but I couldn't figure out the issue with my code.
function charCout(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] === " ")
{
continue;
}
else{
if(str[i] in Object.keys(f))
{
f[str[i]] += 1;
}
else
{
f[str[i]] = 1;
}
}
}
return(f);
}
input: charCout("my name is Khan23")
expected output: {2: 1,3: 1,a: 2,e: 1,h: 1,i: 1,k: 1,m: 2,n: 2,s: 1,y: 1}
what i got: {2: NaN,3: NaN,a: 1,e: 1,h: 1,i: 1,k: 1,m: 1,n: 1,s: 1,y: 1}
Simply see if the property exists by type coercion like: if(f[str[i]])
function charCount(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] === " ")
{
continue;
}
else{
if(f[str[i]])
{
f[str[i]] += 1;
}
else
{
f[str[i]] = 1;
}
}
}
return(f);
}
console.log(charCount('the value is 0000'))
You are using the wrong to way to check if key is in object or not.
You can contract your if-else to single line using || operator.
f[str[i]] = f[str[i]] + 1 || 1
It checks whether f[str[i]] already exists or not. If str[i] is not key of f then f[str[i]] will return undefined and undefined + 1 will be NaN.
NaN is a falsy value so NaN || 1 will evaluate to 1.
If the f[str[i]] exists then it will return any number greater than 0 so it will be set to f[str[i]] + 1(incremented by one)
function charCount(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] !== " ")
{
f[str[i]] = f[str[i]] + 1 || 1
}
}
return(f);
}
console.log(charCount("my name is Khan23"))
Explanation:
You can also use reduce().
const charCount = str => str
.split(' ')
.join('')
.split('')
.reduce((ac,a) =>
(ac[a] = ac[a] + 1 || 1, ac),
{})
console.log(charCount("my name is Khan23"))
using Object.keys(f).includes(str[i]) instead of str[i] in Object.keys(f) is the solution
in operator primarily would work to check if Object provides a particular attribute
Mozilla doc on in and Array.prototype.includes shall help.
following variation of your function would work
function charCout(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] === " ")
{
continue;
}
else{
if(Object.keys(f).includes(str[i]))
{
f[str[i]] += 1;
}
else
{
f[str[i]] = 1;
}
}
}
return(f);
}
You can take advantage of String.protoype.split and Array.prototype.reduce to solve this. Check the comments in the code for details:
const charCount = s => {
//Create an array of characters found in the string (filtering out spaces)
const chars = s.split('').filter(char => char.trim());
//Use reduce to create an occurrence map - increment by 1 each time a character is encountered
return chars.reduce((accum, char) => {
accum[char] = accum[char] ? accum[char] + 1 : 1;
return accum;
}, {});
};
console.log(charCount("my name is Khan23"))
Thank you all for your helpful suggestions. i finally figured out whats wrong with my code.
inputs and outputs of my defected code is as follows:
input: charCout("my name is Khan23")
expected output: {2: 1,3: 1,a: 2,e: 1,h: 1,i: 1,k: 1,m: 2,n: 2,s: 1,y: 1}
what i got: {2: NaN,3: NaN,a: 1,e: 1,h: 1,i: 1,k: 1,m: 1,n: 1,s: 1,y: 1};
I've used "in" operator to find out whether a specific value of string is present the Object.keys(f) array. That's exactly where I went wrong.
after referring to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in i got to know "in" can't be used to filter array using value at index.
You can use str.split("") to get the list of chars and then do something like
const splitted = str.split("");
const result = {};
splitted.map(letter => result[letter] ? result[letter]++ : result[letter] = 1)
it is just an idea

Check if the string is syntactically true

I am trying to solve a task with regex. Given a function with string parameter .The string contains (){}<>[] braces. I have to check if the string is syntactically true and I should also take a count of braces nesting.
This is my version (incomplete)`
const checkBraces = (str) => {
const newStr = str.replace(/[^(){}<>[\]]+/gi, '');
let answer = newStr.match(/\(+\)+|\<+\>+|\{+\}+|\[+\]+/g);
console.log(answer);
}
and this is the minimal count of tests for the function `
checkBraces("---(++++)----") == 0
checkBraces("") == 0
checkBraces("before ( middle []) after ") == 0
checkBraces(") (") == 1
checkBraces("} {") == 1
checkBraces("<( >)") == 1
checkBraces("( [ <> () ] <> )") == 0
checkBraces(" ( [)") == 1
If there is en error so the function should return 1 , else 0 .
In my function I first tried to replace all non-braces so I have a clear string. Now I can't solve this problem .
You can solve this by iterating through the string and keeping a stack of opening braces. Each time you find a closing brace, pop from the stack. The closing brace should match the thing you popped or they're not balanced. At the end the stack should be empty:
let braces = { // lookup to match closing with opening
'(':')',
'{':'}',
'<':'>',
'[':']'
}
let closing = new Set([')', '}', '>', ']']) // quick lookup of brackets
let opening = new Set(['(', '{', '<', '['])
function checkBraces(str) {
/* returns true if balanced, false if unbalanced */
let stack = []
for (l of str){
if (closing.has(l)){ // found a closing bracket
if (l !== braces[stack.pop()]) return false // does it match the last opening?
} else if(opening.has(l)) stack.push(l) // found an opening push to the stack
}
return stack.length === 0
}
console.log(checkBraces("before ( middle []) after "))
console.log(checkBraces("<( >)"))
console.log(checkBraces("( [ <> () ] <> )"))
console.log(checkBraces(" ( [)"))
console.log(checkBraces(" )"))
console.log(checkBraces(" <"))
Not using any inbuilt function except push and pop.
function check(s) {
var arr = [];
for (let i = 0; i < s.length; i++) {
if (s[i] === '{' || s[i] === '[' || s[i] === '(' || s[i] === '<' ) {
arr.push(s[i]);
} else if (s[i] === '}' || s[i] === ']' || s[i] === ')' || s[i] === '>' ) {
if ( arr[arr.length-1] === s[i] ) {
arr.pop();
} else {
return 'not balanced';
}
}
}
if(arr.length) return 'not balanced';
return 'balanced';
}
console.log(check('{781234}[3,4,5,6 ]< >( sdfhniusdf )'));
console.log(check('asssssssssss {}');
console.log(check(' as< habsdj');

Check if quotes and parentheses are balanced

There are multiple solutions to how to check if parentheses are balanced, but I haven't found a single one that would be checking both for balanced quotes and parentheses.
I have been unsuccessfully trying to adapt this solution (codereview - balanced parentheses) to be able to check if the quotes and parentheses are balanced.
For example this should be unbalanced ("back-to-school)"
Original code:
function parenthesesAreBalanced(string) {
var parentheses = "[]{}()",
stack = [],
i, character, bracePosition;
for(i = 0; character = string[i]; i++) {
bracePosition = parentheses.indexOf(character);
if(bracePosition === -1) {
continue;
}
if(bracePosition % 2 === 0) {
stack.push(bracePosition + 1); // push next expected brace position
} else {
if(stack.length === 0 || stack.pop() !== bracePosition) {
return false;
}
}
}
return stack.length === 0;
}
My code - mostly similar - but added an unbalanced quotes check.
function areQuotesAndParenthesesBalanced(s: string): boolean {
const parens = '[]{}()',
parensStack = [];
let index, char, numOfQuotes = 0;
for (index = 0; char = s[index++];){
const bracePosition = parens.indexOf(char);
let braceType;
if (bracePosition === -1 && char !== '"')
continue;
braceType = bracePosition % 2 ? 'closed' : 'open';
//check for double quotes mixed with parentheses
if(char === '"'){
const lastInStack = parensStack[parensStack.length - 1];
numOfQuotes++;
if(lastInStack === '"'){
numOfQuotes--;
parensStack.pop();
}else if(numOfQuotes > 0 && lastInStack !== '"'){
return false;
}else{
parensStack.push('"');
}
}
if (braceType === 'closed') {
if (!parensStack.length || parens.indexOf(parensStack.pop()) != bracePosition - 1)
return false;
} else {
parensStack.push(char);
}
}
//If anything is left on the stack <- not balanced
return !parensStack.length;
}
It is quite tricky for me to determine what's the best approach. With parentheses, you always know when one is open or closed, with quotes, not so much.
function tokensAreBalanced(string) {
var asymmetricTokens = "[]{}()",
symmetricTokens = '"',
stack = [],
i, character, tokenPosition;
for(i = 0; character = string[i]; i++) {
tokenPosition = asymmetricTokens.indexOf(character);
if(tokenPosition >= 0) {
if(tokenPosition % 2 === 0) {
stack.push(asymmetricTokens[tokenPosition + 1]); // push next expected token
} else if(stack.length === 0 || stack.pop() !== character) {
return false;
}
} else {
if(symmetricTokens.includes(character)) {
if(stack.length > 0 && stack[stack.length - 1] === character) {
stack.pop();
} else {
stack.push(character);
}
}
}
}
return stack.length === 0;
}
console.log('("back-to-school)"', tokensAreBalanced('("back-to-school)"'));
console.log('("back-to-school)', tokensAreBalanced('("back-to-school)'));
console.log('("back-to-school")', tokensAreBalanced('("back-to-school")'));
console.log('(ele AND car) OR ("ele car)")', tokensAreBalanced('(ele AND car) OR ("ele car)")'));
This performs a check for push() or pop() of " in 2 ways.
If stack is empty or last character in stack does not equal ", then insert this " into stack.
If stack is not empty and last character in stack is equal to ", then pop() the " in stack itself. This is done because I do a form of greedy matching here since a " for already stack " means expression inside "..." was evaluated. So, we are safe to match these 2 " and proceed with the next.
Works well, but let me know if it fails for any case.
function areQuotesAndParenthesesBalanced(s){
var pairs = {
'}':'{',
']':'[',
')':'(',
};
var stack = [];
for(var i = 0;i < s.length;++i){
switch(s.charAt(i)){
case '[': case '{':case '(':
stack.push(s.charAt(i));
break;
case ']': case '}':case ')':
if(isStackEmpty(stack) || peek(stack) !== pairs[s.charAt(i)]) return false;
stack.pop();
break;
case '"':
if(isStackEmpty(stack) || peek(stack) !== s.charAt(i)){
stack.push(s.charAt(i));
}else{
stack.pop();
}
}
}
return isStackEmpty(stack);
}
function isStackEmpty(s){
return s.length === 0;
}
function peek(s){
return s[s.length-1];
}
var tests = {
'("back-to-school")':true,
'"(back-to-school)"':true,
'("back-to-school)"':false,
'("back-to-school)':false,
'"["["["[]"]"]"]"':true,
'"["]""':false,
'"[]"""':true,
'""""':true,
'""':true,
'"':false,
'""[("")]""':true,
'""[("")]':true,
'"["["["[]"]"[""]]"]':false,
'"[]"[({})]""':true,
'"[{}"]':false
};
for(var each_test in tests){
var res = areQuotesAndParenthesesBalanced(each_test);
console.log(each_test + " --> " + (res === tests[each_test] ? "ok" : "not ok") + " , expected : " + tests[each_test]);
}
OUTPUT
("back-to-school") --> ok , expected : true
"(back-to-school)" --> ok , expected : true
("back-to-school)" --> ok , expected : false
("back-to-school) --> ok , expected : false
"["["["[]"]"]"]" --> ok , expected : true
"["]"" --> ok , expected : false
"[]""" --> ok , expected : true
"""" --> ok , expected : true
"" --> ok , expected : true
" --> ok , expected : false
""[("")]"" --> ok , expected : true
""[("")] --> ok , expected : true
"["["["[]"]"[""]]"] --> ok , expected : false
"[]"[({})]"" --> ok , expected : true
"[{}"] --> ok , expected : false
You could try putting an ordered tuple on the stack and checking based off of that.
[(,"],
[",)],
[(,"],
[",)]
== ("")("") example of a balanced stack.
[",(],
[",(],
[),"],
[),"]
== "("()")" another balanced stack
[(,"],
[),"]
== (")" trivial unbalanced stack
[(,)] <- trivial item, can ignore in implementation
[","] <- trivial item, can ignore in implementation
[",(],
[),(],
[),"]
== "()()" balanced stack
I'm too tired to actually implement this, but hopefully it gave you some ideas and illustrative examples, I'll revisit it after I get some sleep.
I would suggest simplifying the input by removing all quoted substrings. Quotes behave differently because parentheses inside quotes are not treated specially, but as normal characters. By first removing each quoted part, this "problem" is resolved from the start. Also irrelevant characters (that are not parentheses nor quotes) can be removed as well. That way we are only left with a string that has parentheses and possibly a single occurrence of a quote. The latter would immediately make the input invalid.
Here is code similar to #nice_dev's answer, with that manipulation implemented and with pairs inversed; it also deals with apostrophe as alternative delimiter:
function areQuotesAndParenthesesBalanced(s) {
const pairs = {
'{':'}',
'[':']',
'(':')',
};
const stack = [""]; // Dummy
for (const token of s.replace(/"[^"]*"|'[^']*'|[^"'{}()[\]]+/g, "")) {
if (token in pairs) {
stack.push(pairs[token]);
} else {
if (stack.at(-1) !== token) return false;
stack.pop();
}
}
return stack.length == 1; // Empty
}
// Same tests as in the answer of #nice_dev:
const tests = {
'("back-to-school")': true,
'"(back-to-school)"': true,
'("back-to-school)"': false,
'("back-to-school)': false,
'"["["["[]"]"]"]"': true,
'"["]""': false,
'"[]"""': true,
'""""': true,
'""': true,
'"': false,
'""[("")]""': true,
'""[("")]': true,
'"["["["[]"]"[""]]"]': false,
'"[]"[({})]""': true,
'"[{}"]': false
};
for (const each_test in tests){
const res = areQuotesAndParenthesesBalanced(each_test);
console.log(`${each_test} --> ${res === tests[each_test] ? "ok" : "not ok"}, expected: ${tests[each_test]}`);
}
A simple approaching way might be like this for only first braces:
function Search(str) {
const onlyBrackets = str.replace(/[a-zA-Z]/g, "");
const left = onlyBrackets.replace(/[)]/g, "");
const right = onlyBrackets.replace(/[(]/g, "");
str = left.length === right.length ? 1 : 0
return str
}
console.log(Search("(coder)(byte))")) // 0
console.log(Search("(c(oder))b(yte)")) // 1
function isParenthesisBalanced(_str) {
var parenMap = {'{':'}', '[':']', '(':')'};
var parenStack =[];
for(var i=0;i<_str.length; i++) {
if(_str[i] in parenMap) {
parenStack.push(_str[i]);
} else if(Object.values(parenMap).indexOf(_str[i]) != -1) {
if(parenMap[parenStack.pop()] != _str[i]) return false;
}
}
return true;
}

Categories