Check if it is possible to return to the starting position after moving in the given directions | Java implementation of above approach ; Main method ; int n = 0 ; Count of North int s = 0 ; Count of South int e = 0 ; Count of East int w = 0 ; Count of West
public class GFG { public static void main ( String args [ ] ) { String st = " NNNWEWESSS " ; int len = st . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( st . charAt ( i ) == ' N ' ) n += 1 ; if ( st . charAt ( i ) == ' S ' ) s += 1 ; if ( st . charAt ( i ) == ' W ' ) w += 1 ; if ( st . charAt ( i ) == ' E ' ) e += 1 ; } if ( n == s && w == e ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }