text: Find the direction from given string | Java implementation of above approach ; Function to find the final direction ; if count is positive that implies resultant is clockwise direction ; if count is negative that implies resultant is anti - clockwise direction ; Driver code
code: import java . util . * ; class GFG { static String findDirection ( String s ) { int count = 0 ; String d = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( 0 ) == 'NEW_LINE') return null ; if ( s . charAt ( i ) == ' L ' ) count -- ; else { if ( s . charAt ( i ) == ' R ' ) count ++ ; } } if ( count > 0 ) { if ( count % 4 == 0 ) d = " N " ; else if ( count % 4 == 1 ) d = " E " ; else if ( count % 4 == 2 ) d = " S " ; else if ( count % 4 == 3 ) d = " W " ; } if ( count < 0 ) { if ( count % 4 == 0 ) d = " N " ; else if ( count % 4 == - 1 ) d = " W " ; else if ( count % 4 == - 2 ) d = " S " ; else if ( count % 4 == - 3 ) d = " E " ; } return d ; } public static void main ( String [ ] args ) { String s = " LLRLRRL " ; System . out . println ( findDirection ( s ) ) ; s = " LL " ; System . out . println ( findDirection ( s ) ) ; } }