<pre><code>
Consider a string expression consisting of the characters < and > only. We consider the string to be balanced if each < always appears before (i.e., to the left of) a corresponding > character (they do not need to be adjacent). Moreover, each < and > act as a unique pair of symbols and neither symbol can be considered as part of any other pair of symbols. For example, the strings <<>>, <>, and <><> are all balanced, but the strings >>, <<>, and ><>< are unbalanced.
To balance a string, we can replace only > character with <> at most maxReplacement times. Given an expression and the value of maxReplacement, can you turn an unbalanced string into a balanced one?
Complete the balancedOrNot function in the editor below. It has the following parameters:
An array of n strings, expressions, denoting the list of expressions to check.
An array of n integers, maxReplacements, where maxReplacementsi denotes the maximum number of replacements allowed when attempting to balance expressionsi.
The function must return an array of integers where each index i (0 ≤ i < n) contains a 1 if expressionsi is balanced or a 0 if it is not.
Input Format:
A set of internal unit tests will be on the code with input in the following format.
The first line contains an integer, n, denoting the size of expressions.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains a string describing expressionsi.
The next line contains an integer, m, denoting the size of maxReplacements.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains a string describing maxReplacementsi.
Constraints
1 ≤ n ≤ 102
1 ≤ length(expressionsi) ≤ 105
0 ≤ maxReplacementsi ≤ 105
Output Format:
The function must return an array of integers where each index i (0 ≤ i < n) contains a 1 if expressionsi is balanced or a 0 if it is not.
Observations:
Check that your code runs before submitting it!
</code></pre>
<h5>
Output
</h5>
<div id="konsole"></div>
JavaScript
function BalancedOrNot(expressions, maxReplacements) {
let results = [];
var countOccurrence = function(str, pattern) {
return (str.match(new RegExp(pattern, 'g')) || []).length;
}
for (let i = 0; i < expressions.length; i++) {
let numBalanced = countOccurrence(expressions[i], '<>'),
numUnbalanced = expressions[i].length - numBalanced * 2,
numLt = countOccurrence(expressions[i], '<'),
numGt = countOccurrence(expressions[i], '>'),
isBalanceable = 1;
if (numUnbalanced > 0) {
if (numLt - numBalanced > 0) {
isBalanceable = 0;
} else {
isBalanceable = (numGt - numBalanced > maxReplacements[i]) ? 0 : 1;
}
}
results.push(isBalanceable);
}
return results; //it must return an array of integers.
}
var konsole = document.getElementById('konsole');
konsole.innerHTML = " $ " + BalancedOrNot(['<>>>', '<>>>>'], [2, 2]);
konsole.innerHTML += "<br> $ " + BalancedOrNot(['<>', '<>><'], [1, 0]);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.