Verify preorder sequence of Binary Search Tree (BST) Interview Question Hacker Rank
import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.Stack; public class BracesCheck public static void main(String args[]) Scanner in = new ); System.out.println(Please Enter Your String); String inputString = in.nextLine(); System.out.println(checkBraces(inputString)); in.close(); static String checkBraces(String value) StackCharacter specialCharStack = new StackCharacter(); String response = Fail; char tempChar; Character[] openingBraces = [,(,; Character[] closingBraces = ],),; ListCharacter openingBracesList = Arrays.asList(openingBraces); ListCharacter closingBracesList = Arrays.asList(closingBraces); if(value == null) return response; else if(value.length()==0) response = Pass; else for(int i=0; i value.length(); i++) tempChar = value.charAt(i); ntains(tempChar)) specialCharStack.push(tempChar); else if(closingBracesList.contains(tempChar)) if(!specialCharStack.isEmpty()) if(tempChar==) && ( != specialCharStack.pop()) return response; else if(tempChar== && !=specialCharStack.pop()) return response; else if(tempChar==] && [ != specialCharStack.pop()) return response; else return response; else return response; if( specialCharStack.isEmpty()) response = Pass; return response; else return response;
Posted inAllInterview Questionsand taggedCoding ChallengeHacker RankHacker Rank coding challengeInterview QuestionjavaJava Stack questionsParentheses parserParenthesis check
Your email address will not be published.Required fields are marked*
So the basic approach would be to use stack to verify the correct order of braces.
Input: String of braces only:[]()[()()]
Please Enter Your String () Pass Please Enter Your String ((())) Pass Please Enter Your String ((((((( Fail Please Enter Your String )))))((((( Fail Please Enter Your String )))))) Fail
Calculate Fibonacci number in java using Recursion and Multi Threading
Input: String of braces only:[](()()]
Below is the Screenshots of the project structure followed by the source code:
Iterate over each braces from start to end and do the below step at each iteration.
Even or Odd String Interview Question Hacker Rank
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.
Step 1. When you find opening bracket: Do Push.
When Binary Search Tree can perform worst?
Match the braces and verify weather all the opening braces has the closing braces in right order.