text: Total time required to travel a path denoted by a given string | C # program for the above approach ; Function to calculate time taken to travel the path ; Stores total time ; Initial position ; Stores visited segments ; Check whether segment is present in the set ; Increment the value of time by 2 ; Insert segment into the set ; Print the value of time ; Driver Code
code: using System ; using System . Collections . Generic ; class GFG { static void calcTotalTime ( string path ) { int time = 0 ; int x = 0 , y = 0 ; HashSet < string > s = new HashSet < string > ( ) ; for ( int i = 0 ; i < path . Length ; i ++ ) { int p = x ; int q = y ; if ( path [ i ] == ' N ' ) y ++ ; else if ( path [ i ] == ' S ' ) y -- ; else if ( path [ i ] == ' E ' ) x ++ ; else if ( path [ i ] == ' W ' ) x -- ; string o = ( p + x ) + " ▁ " + ( q + y ) ; if ( s . Contains ( o ) == false ) { time += 2 ; s . Add ( o ) ; } else time += 1 ; } Console . Write ( time ) ; } public static void Main ( ) { string path = " NSE " ; calcTotalTime ( path ) ; } }