
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Parse and balance angle brackets problem in JavaScript
We are given a string of angle brackets, and we are required to write a function that add brackets at the beginning and end of the string to make all brackets match.
The angle brackets match if for every < there is a corresponding > and for every > there is a corresponding <.
For example − If the input string is −
const str = '><<><';
Output
Then the output should be −
const output = '<><<><>>';
Here, we added, '<' at the beginning and '>>' at the end to balance the string.
We will use a number that will keep count of the number of open '<' tags so far. And then, when we encounter a '>' tag, if there are no current open tags, we will add '<' to the beginning of the string (while keeping the open tag count at 0).
Then, at the end, add a number of '>'s matching the number of currently open tags.
Example
The code for this will be −
const str = '><<><'; const buildPair = (str = '') => { let count = 0; let extras = 0; for (const char of str) { if (char === '>') { if (count === 0) { extras++; } else { count−−; }; } else { count++; }; }; const leadingTags = '<'.repeat(extras); const trailingTags = '>'.repeat(count); return leadingTags + str + trailingTags; }; console.log(buildPair(str));
Output
And the output in the console will be −
><<><>>
- Related Articles
- Finding the balance of brackets in JavaScript
- Balance a string after removing extra brackets in C++
- Finding score of brackets in JavaScript
- Validating brackets in a string in JavaScript
- Find digits between brackets in JavaScript RegExp?
- Read by key and parse as JSON in JavaScript
- Solve the Sherlock and Array problem in JavaScript
- JavaScript JSON parse() Method
- JavaScript RegExp return values between various square brackets? How to get value brackets?
- Snail Trail Problem in JavaScript
- Recursive Staircase problem in JavaScript
- Distributing Bananas Problem in JavaScript
- How to find character between brackets in JavaScript RegExp?
- Parse array to equal intervals in JavaScript
- How to parse JSON object in JavaScript?
