text: Count of strings that does not contain Arc intersection | Java program for the above approach ; Function to check if there is arc intersection or not ; Traverse the String S ; Insert all the elements in the stack one by one ; Extract the top element ; Pop out the top element ; Check if the top element is same as the popped element ; Otherwise ; If the stack is empty ; Function to check if there is arc intersection or not for the given array of Strings ; Stores count of String not having arc intersection ; Iterate through array ; Length of every String ; Function Call ; Print the desired count ; Driver Code ; Function Call
code: import java . util . * ; class GFG { static int arcIntersection ( String S , int len ) { Stack < Character > stk = new Stack < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { stk . push ( S . charAt ( i ) ) ; if ( stk . size ( ) >= 2 ) { char temp = stk . peek ( ) ; stk . pop ( ) ; if ( stk . peek ( ) == temp ) { stk . pop ( ) ; } else { stk . add ( temp ) ; } } } if ( stk . isEmpty ( ) ) return 1 ; return 0 ; } static void countString ( String arr [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int len = arr [ i ] . length ( ) ; count += arcIntersection ( arr [ i ] , len ) ; } System . out . print ( count + "NEW_LINE"); } public static void main ( String [ ] args ) { String arr [ ] = { "0101" , "0011" , "0110" } ; int N = arr . length ; countString ( arr , N ) ; } }