Parentheses / Brackets Check (Hacker Rank Interview Questions)

Match the braces and verify whether all the opening braces has the closing braces in right order.

Input: String of braces only: {}[{{()}}()]”
Output: Yes

Input: String of braces only: “[{}]({{()}}()]”
Output: No

So the basic approach would be to use a stack to verify the correct order of braces.

Iterate over each brace from start to end and do the below step at each iteration.

Step 1.  When you find opening bracket: Do Push.
Step 2.  When you find closing bracket: Do pop and check whether closing and opening bracket match. If yes, then continue, if not then break and return NO.

Below is the Screenshots of the project structure followed by the source code:

You can have below code from GIT.

GitHub-Mark-32px https://github.com/Niravkumar-Patel/SnippetExampleRepo.git

Project Structure in Eclipse:

HackerRankProjectStructure

Below is the code:

The output of the above code

You can have above code from GIT.

GitHub-Mark-32px https://github.com/Niravkumar-Patel/SnippetExampleRepo.git

Leave a Reply

Your email address will not be published. Required fields are marked *