codeparrot/xlcost-text-to-code huggingface dataset:
Convert a Binary Tree into its Mirror Tree | Iterative Java program to convert a Binary Tree to its mirror ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; Do BFS . While doing BFS , keep swapping left and right children ; pop top node from queue ; swap left child with right child ; push left and right children ; Helper function to print Inorder traversal . ; Driver code ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void mirror ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { Node curr = q . peek ( ) ; q . remove ( ) ; Node temp = curr . left ; curr . left = curr . right ; curr . right = temp ; ; if ( curr . left != null ) q . add ( curr . left ) ; if ( curr . right != null ) q . add ( curr . right ) ; } } static void inOrder ( Node node ) { if ( node == null ) return ; inOrder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inOrder ( node . right ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; System . out . print ( " Inorder traversal of the " ▁ + " coned tree is "); inOrder ( root ) ; mirror ( root ) ; System . out . print ( " Inorder traversal of the " + ▁ " mirror tree is "); inOrder ( root ) ; } }
Delete middle element of a stack | Java code to delete middle of a stack without using additional data structure . ; Deletes middle of stack of size n . Curr is current item number ; If stack is empty or all items are traversed ; Remove current item ; Remove other items ; Put all items back except middle ; Driver function to test above functions ; push elements into the stack ; Printing stack after deletion of middle .
import java . io . * ; import java . util . * ; public class GFG { static void deleteMid ( Stack < Character > st , int n , int curr ) { if ( st . empty ( ) curr == n ) return ; char x = st . pop ( ) ; deleteMid ( st , n , curr + 1 ) ; if ( curr != n / 2 ) st . push ( x ) ; } public static void main ( String args [ ] ) { Stack < Character > st = new Stack < Character > ( ) ; st . push ( '1' ) ; st . push ( '2' ) ; st . push ( '3' ) ; st . push ( '4' ) ; st . push ( '5' ) ; st . push ( '6' ) ; st . push ( '7' ) ; deleteMid ( st , st . size ( ) , 0 ) ; while ( ! st . empty ( ) ) { char p = st . pop ( ) ; System . out . print ( p + " ▁ " ) ; } } }
Sorting array using Stacks | Java program to sort an array using stack ; This function return the sorted stack ; pop out the first element ; while temporary stack is not empty and top of stack is smaller than temp ; pop from temporary stack and push it to the input stack ; push temp in tempory of stack ; push array elements to stack ; Sort the temporary stack ; Put stack elements in arrp [ ] ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static Stack < Integer > sortStack ( Stack < Integer > input ) { Stack < Integer > tmpStack = new Stack < Integer > ( ) ; while ( ! input . empty ( ) ) { int tmp = input . peek ( ) ; input . pop ( ) ; while ( ! tmpStack . empty ( ) && tmpStack . peek ( ) < tmp ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } return tmpStack ; } static void sortArrayUsingStacks ( int [ ] arr , int n ) { Stack < Integer > input = new Stack < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) input . push ( arr [ i ] ) ; Stack < Integer > tmpStack = sortStack ( input ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = tmpStack . peek ( ) ; tmpStack . pop ( ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 5 , 15 , 45 } ; int n = arr . length ; sortArrayUsingStacks ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Reverse individual words | Java program to reverse individual words in a given string using STL list ; reverses individual words of a string ; Traverse given string and push all characters to stack until we see a space . ; When we see a space , we print contents of stack . ; Since there may not be space after last word . ; Driver program to test above function
import java . io . * ; import java . util . * ; class GFG { static void reverseWords ( String str ) { Stack < Character > st = new Stack < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( str . charAt ( i ) != ' ▁ ' ) st . push ( str . charAt ( i ) ) ; else { while ( st . empty ( ) == false ) { System . out . print ( st . pop ( ) ) ; } System . out . print ( " ▁ " ) ; } } while ( st . empty ( ) == false ) { System . out . print ( st . pop ( ) ) ; } } public static void main ( String [ ] args ) { String str = " Geeks ▁ for ▁ Geeks " ; reverseWords ( str ) ; } }
Count subarrays where second highest lie before highest | Java program to count number of distinct instance where second highest number lie before highest number in all subarrays . ; Pair class ; Finding the next greater element of the array . ; Finding the previous greater element of the array . ; Wrapper Function ; Finding previous largest element ; Finding next largest element ; Driver code
import java . util . * ; class GFG { static int MAXN = 100005 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeNext ( int arr [ ] , int n , int nextBig [ ] ) { Stack < pair > s = new Stack < > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { nextBig [ i ] = i ; while ( ! s . empty ( ) && s . peek ( ) . first < arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) nextBig [ i ] = s . peek ( ) . second ; s . push ( new pair ( arr [ i ] , i ) ) ; } } static void makePrev ( int arr [ ] , int n , int prevBig [ ] ) { Stack < pair > s = new Stack < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { prevBig [ i ] = - 1 ; while ( ! s . empty ( ) && s . peek ( ) . first < arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) prevBig [ i ] = s . peek ( ) . second ; s . push ( new pair ( arr [ i ] , i ) ) ; } } static int wrapper ( int arr [ ] , int n ) { int [ ] nextBig = new int [ MAXN ] ; int [ ] prevBig = new int [ MAXN ] ; int [ ] maxi = new int [ MAXN ] ; int ans = 0 ; makePrev ( arr , n , prevBig ) ; makeNext ( arr , n , nextBig ) ; for ( int i = 0 ; i < n ; i ++ ) if ( nextBig [ i ] != i ) maxi [ nextBig [ i ] - i ] = Math . max ( maxi [ nextBig [ i ] - i ] , i - prevBig [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) ans += maxi [ i ] ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int n = arr . length ; System . out . println ( wrapper ( arr , n ) ) ; } }
Largest Rectangular Area in a Histogram | Set 2 | Java program to find maximum rectangular area in linear time ; The main function to find the maximum rectangular area under given histogram with n bars ; Create an empty stack . The stack holds indexes of hist [ ] array The bars stored in stack are always in increasing order of their heights . ; Initialize max area ; To store top of stack ; To store area with top bar as the smallest bar ; Run through all bars of given histogram ; If this bar is higher than the bar on top stack , push it to stack ; If this bar is lower than top of stack , then calculate area of rectangle with stack top as the smallest ( or minimum height ) bar . ' i ' is ' right ▁ index ' for the top and element before top in stack is ' left ▁ index ' ; store the top index ; pop the top ; Calculate the area with hist [ tp ] stack as smallest bar ; update max area , if needed ; Now pop the remaining bars from stack and calculate area with every popped bar as the smallest bar ; Driver program to test above function
import java . util . Stack ; public class RectArea { static int getMaxArea ( int hist [ ] , int n ) { Stack < Integer > s = new Stack < > ( ) ; int max_area = 0 ; int tp ; int area_with_top ; int i = 0 ; while ( i < n ) { if ( s . empty ( ) || hist [ s . peek ( ) ] <= hist [ i ] ) s . push ( i ++ ) ; else { tp = s . peek ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } } while ( s . empty ( ) == false ) { tp = s . peek ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } return max_area ; } public static void main ( String [ ] args ) { int hist [ ] = { 6 , 2 , 5 , 4 , 5 , 1 , 6 } ; System . out . println ( " Maximum ▁ area ▁ is ▁ " + getMaxArea ( hist , hist . length ) ) ; } }
Program for Tower of Hanoi | JAVA recursive function to solve tower of hanoi puzzle ; Driver code ; Number of disks ; A , B and C are names of rods
import java . util . * ; import java . io . * ; import java . math . * ; class GFG { static void towerOfHanoi ( int n , char from_rod , char to_rod , char aux_rod ) { if ( n == 1 ) { System . out . println ( " Move ▁ disk ▁ 1 ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; return ; } towerOfHanoi ( n - 1 , from_rod , aux_rod , to_rod ) ; System . out . println ( " Move ▁ disk ▁ " + n + " ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; towerOfHanoi ( n - 1 , aux_rod , to_rod , from_rod ) ; } public static void main ( String args [ ] ) { int n = 4 ; towerOfHanoi ( n , ' A ' , ' C ' , ' B ' ) ; } }
Find maximum of minimum for every window size in a given array | A naive method to find maximum of minimum of all windows of different sizes ; Consider all windows of different sizes starting from size 1 ; Initialize max of min for current window size k ; Traverse through all windows of current size k ; Find minimum of current window ; Update maxOfMin if required ; Print max of min for current window size ; Driver method
class Test { static int arr [ ] = { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; static void printMaxOfMin ( int n ) { for ( int k = 1 ; k <= n ; k ++ ) { int maxOfMin = Integer . MIN_VALUE ; for ( int i = 0 ; i <= n - k ; i ++ ) { int min = arr [ i ] ; for ( int j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] < min ) min = arr [ i + j ] ; } if ( min > maxOfMin ) maxOfMin = min ; } System . out . print ( maxOfMin + " ▁ " ) ; } } public static void main ( String [ ] args ) { printMaxOfMin ( arr . length ) ; } }
Find maximum of minimum for every window size in a given array | An efficient Java program to find maximum of all minimums of windows of different size ; Used to find previous and next smaller ; Arrays to store previous and next smaller ; Initialize elements of left [ ] and right [ ] ; Fill elements of left [ ] using logic discussed on www . geeksforgeeks . org / next - greater - element / https : ; Empty the stack as stack is going to be used for right [ ] ; Fill elements of right [ ] using same logic ; Create and initialize answer array ; Fill answer array by comparing minimums of all lengths computed using left [ ] and right [ ] ; length of the interval ; arr [ i ] is a possible answer for this length ' len ' interval , check if arr [ i ] is more than max for ' len ' ; Some entries in ans [ ] may not be filled yet . Fill them by taking values from right side of ans [ ] ; Print the result ; Driver method
import java . util . Stack ; class Test { static int arr [ ] = { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; static void printMaxOfMin ( int n ) { Stack < Integer > s = new Stack < > ( ) ; int left [ ] = new int [ n + 1 ] ; int right [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = - 1 ; right [ i ] = n ; } for ( int i = 0 ; i < n ; i ++ ) { while ( ! s . empty ( ) && arr [ s . peek ( ) ] >= arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) left [ i ] = s . peek ( ) ; s . push ( i ) ; } while ( ! s . empty ( ) ) s . pop ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ! s . empty ( ) && arr [ s . peek ( ) ] >= arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) right [ i ] = s . peek ( ) ; s . push ( i ) ; } int ans [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) ans [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = right [ i ] - left [ i ] - 1 ; ans [ len ] = Math . max ( ans [ len ] , arr [ i ] ) ; } for ( int i = n - 1 ; i >= 1 ; i -- ) ans [ i ] = Math . max ( ans [ i ] , ans [ i + 1 ] ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( ans [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { printMaxOfMin ( arr . length ) ; } }
Length of the longest valid substring | Java program to find length of the longest valid substring ; method to get length of the longest valid ; Create a stack and push - 1 as initial index to it . ; Initialize result ; Traverse all characters of given string ; If opening bracket , push index of it ; If closing bracket , i . e . , str [ i ] = ' ) ' ; Pop the previous opening bracket 's index ; Check if this length formed with base of current valid substring is more than max so far ; If stack is empty . push current index as base for next valid substring ( if any ) ; Driver code ; Function call ; Function call
import java . util . Stack ; class Test { static int findMaxLen ( String str ) { int n = str . length ( ) ; Stack < Integer > stk = new Stack < > ( ) ; stk . push ( - 1 ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) stk . push ( i ) ; else { if ( ! stk . empty ( ) ) stk . pop ( ) ; if ( ! stk . empty ( ) ) result = Math . max ( result , i - stk . peek ( ) ) ; else stk . push ( i ) ; } } return result ; } public static void main ( String [ ] args ) { String str = " ( ( ( ) ( ) " ; System . out . println ( findMaxLen ( str ) ) ; str = " ( ) ( ( ) ) ) ) ) " ; System . out . println ( findMaxLen ( str ) ) ; } }
Convert a tree to forest of even nodes | Java program to find maximum number to be removed to convert a tree into forest containing trees of even number of nodes ; Return the number of nodes of subtree having node as a root . ; Mark node as visited . ; Traverse the adjacency list to find non - visited node . ; Finding number of nodes of the subtree of a subtree . ; If nodes are even , increment number of edges to removed . Else leave the node as child of subtree . ; Return the maximum number of edge to remove to make forest . ; Driven Program
import java . util . * ; class GFG { static int N = 12 , ans ; static Vector < Vector < Integer > > tree = new Vector < Vector < Integer > > ( ) ; static int dfs ( int visit [ ] , int node ) { int num = 0 , temp = 0 ; visit [ node ] = 1 ; for ( int i = 0 ; i < tree . get ( node ) . size ( ) ; i ++ ) { if ( visit [ tree . get ( node ) . get ( i ) ] == 0 ) { temp = dfs ( visit , tree . get ( node ) . get ( i ) ) ; if ( temp % 2 != 0 ) num += temp ; else ans ++ ; } } return num + 1 ; } static int minEdge ( int n ) { int visit [ ] = new int [ n + 2 ] ; ans = 0 ; dfs ( visit , 1 ) ; return ans ; } public static void main ( String args [ ] ) { int n = 10 ; for ( int i = 0 ; i < n + 2 ; i ++ ) tree . add ( new Vector < Integer > ( ) ) ; tree . get ( 1 ) . add ( 3 ) ; tree . get ( 3 ) . add ( 1 ) ; tree . get ( 1 ) . add ( 6 ) ; tree . get ( 6 ) . add ( 1 ) ; tree . get ( 1 ) . add ( 2 ) ; tree . get ( 2 ) . add ( 1 ) ; tree . get ( 3 ) . add ( 4 ) ; tree . get ( 4 ) . add ( 3 ) ; tree . get ( 6 ) . add ( 8 ) ; tree . get ( 8 ) . add ( 6 ) ; tree . get ( 2 ) . add ( 7 ) ; tree . get ( 7 ) . add ( 2 ) ; tree . get ( 2 ) . add ( 5 ) ; tree . get ( 5 ) . add ( 2 ) ; tree . get ( 4 ) . add ( 9 ) ; tree . get ( 9 ) . add ( 4 ) ; tree . get ( 4 ) . add ( 10 ) ; tree . get ( 10 ) . add ( 4 ) ; System . out . println ( minEdge ( n ) ) ; } }
Length of the longest valid substring | Java program to find length of the longest valid subString ; Initialize curMax to zero ; Iterate over the String starting from second index ; Driver code ; Function call ; Function call
import java . util . * ; class GFG { static int findMaxLen ( String s ) { if ( s . length ( ) <= 1 ) return 0 ; int curMax = 0 ; int [ ] longest = new int [ s . length ( ) ] ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' ) ' && i - longest [ i - 1 ] - 1 >= 0 && s . charAt ( i - longest [ i - 1 ] - 1 ) == ' ( ' ) { longest [ i ] = longest [ i - 1 ] + 2 + ( ( i - longest [ i - 1 ] - 2 >= 0 ) ? longest [ i - longest [ i - 1 ] - 2 ] : 0 ) ; curMax = Math . max ( longest [ i ] , curMax ) ; } } return curMax ; } public static void main ( String [ ] args ) { String str = " ( ( ( ) ( ) " ; System . out . print ( findMaxLen ( str ) + "NEW_LINE"); str = " ( ) ( ( ) ) ) ) ) " ; System . out . print ( findMaxLen ( str ) + "NEW_LINE"); } }
Length of the longest valid substring | Java program to implement the above approach ; Function to return the length of the longest valid substring ; Variables for left and right counter maxlength to store the maximum length found so far ; Iterating the string from left to right ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Iterating the string from right to left ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Driver code ; Function call
import java . util . Scanner ; import java . util . Arrays ; class GFG { public static int solve ( String s , int n ) { int left = 0 , right = 0 ; int maxlength = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = Math . max ( maxlength , 2 * right ) ; else if ( right > left ) left = right = 0 ; } left = right = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == ' ( ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = Math . max ( maxlength , 2 * left ) ; else if ( left > right ) left = right = 0 ; } return maxlength ; } public static void main ( String args [ ] ) { System . out . print ( solve ( " ( ( ( ) ( ) ( ) ( ) ( ( ( ( ) ) " , 16 ) ) ; } }
Expression contains redundant bracket or not | Java Program to check whether valid expression is redundant or not ; Function to check redundant brackets in a balanced expression ; create a stack of characters ; Iterate through the given expression ; if current character is close parenthesis ' ) ' ; If immediate pop have open parenthesis ' ( ' duplicate brackets found ; Check for operators in expression ; Fetch top element of stack ; If operators not found ; push open parenthesis ' ( ' , ; operators and operands to stack ; Function to check redundant brackets ; Driver code
import java . util . Stack ; public class GFG { static boolean checkRedundancy ( String s ) { Stack < Character > st = new Stack < > ( ) ; char [ ] str = s . toCharArray ( ) ; for ( char ch : str ) { if ( ch == ' ) ' ) { char top = st . peek ( ) ; st . pop ( ) ; boolean flag = true ; while ( top != ' ( ' ) { if ( top == ' + ' top == ' - ' top == ' * ' top == ' / ' ) { flag = false ; } top = st . peek ( ) ; st . pop ( ) ; } if ( flag == true ) { return true ; } } else { st . push ( ch ) ; } } return false ; } static void findRedundant ( String str ) { boolean ans = checkRedundancy ( str ) ; if ( ans == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String str = " ( ( a + b ) ) " ; findRedundant ( str ) ; str = " ( a + ( b ) / c ) " ; findRedundant ( str ) ; str = " ( a + b * ( c - d ) ) " ; findRedundant ( str ) ; } }
Check if two expressions with brackets are same | Java program to check if two expressions evaluate to same . ; Return local sign of the operand . For example , in the expr a - b - ( c ) , local signs of the operands are + a , - b , + c ; Evaluate expressions into the count vector of the 26 alphabets . If add is true , then add count to the count vector of the alphabets , else remove count from the count vector . ; stack stores the global sign for operands . ; + means true global sign is positive initially ; global sign for the bracket is pushed to the stack ; global sign is popped out which was pushed in for the last bracket ; global sign is positive ( we use different values in two calls of functions so that we finally check if all vector elements are 0. ; global sign is negative here ; Returns true if expr1 and expr2 represent same expressions ; Create a vector for all operands and initialize the vector as 0. ; Put signs of all operands in expr1 ; Subtract signs of operands in expr2 ; If expressions are same , vector must be 0. ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static final int MAX_CHAR = 26 ; static boolean adjSign ( String s , int i ) { if ( i == 0 ) return true ; if ( s . charAt ( i - 1 ) == ' - ' ) return false ; return true ; } ; static void eval ( String s , int [ ] v , boolean add ) { Stack < Boolean > stk = new Stack < > ( ) ; stk . push ( true ) ; int i = 0 ; while ( i < s . length ( ) ) { if ( s . charAt ( i ) == ' + ' || s . charAt ( i ) == ' - ' ) { i ++ ; continue ; } if ( s . charAt ( i ) == ' ( ' ) { if ( adjSign ( s , i ) ) stk . push ( stk . peek ( ) ) ; else stk . push ( ! stk . peek ( ) ) ; } else if ( s . charAt ( i ) == ' ) ' ) stk . pop ( ) ; else { if ( stk . peek ( ) ) v [ s . charAt ( i ) - ' a ' ] += ( adjSign ( s , i ) ? add ? 1 : - 1 : add ? - 1 : 1 ) ; else v [ s . charAt ( i ) - ' a ' ] += ( adjSign ( s , i ) ? add ? - 1 : 1 : add ? 1 : - 1 ) ; } i ++ ; } } ; static boolean areSame ( String expr1 , String expr2 ) { int [ ] v = new int [ MAX_CHAR ] ; eval ( expr1 , v , true ) ; eval ( expr2 , v , false ) ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( v [ i ] != 0 ) return false ; return true ; } public static void main ( String [ ] args ) { String expr1 = " - ( a + b + c ) " , expr2 = " - a - b - c " ; if ( areSame ( expr1 , expr2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find index of closing bracket for a given opening bracket in an expression | Java program to find index of closing bracket for given opening bracket . ; Function to find index of closing bracket for given opening bracket . ; If index given is invalid and is not an opening bracket . ; Stack to store opening brackets . ; Traverse through string starting from given index . ; If current character is an opening bracket push it in stack . ; If current character is a closing bracket , pop from stack . If stack is empty , then this closing bracket is required bracket . ; If no matching closing bracket is found . ; Driver Code ; should be 8 ; should be 7 ; should be 12 ; No matching bracket
import java . util . Stack ; class GFG { static void test ( String expression , int index ) { int i ; if ( expression . charAt ( index ) != ' [ ' ) { System . out . print ( expression + " , ▁ " + index + ": -1NEW_LINE"); return ; } Stack < Integer > st = new Stack < > ( ) ; for ( i = index ; i < expression . length ( ) ; i ++ ) { if ( expression . charAt ( i ) == ' [ ' ) { st . push ( ( int ) expression . charAt ( i ) ) ; } else if ( expression . charAt ( i ) == ' ] ' ) { st . pop ( ) ; if ( st . empty ( ) ) { System . out . print ( expression + " , ▁ " + index + " : ▁ " + i + "NEW_LINE"); return ; } } } System . out . print ( expression + " , ▁ " + index + ": -1NEW_LINE"); } public static void main ( String [ ] args ) { test ( " [ ABC [ 23 ] ] [89 ] " , 0 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 4 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 9 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 1 ) ; } }