Modified DFS in which no edge is traversed twice
static void dfs ( String node , int k , String A ) { for ( int i = 0 ; i < k ; ++ i ) { String str = node + A [ i ] ; if ( ! seen . Contains ( str ) ) { seen . Add ( str ) ; dfs ( str . Substring ( 1 ) , k , A ) ; edges . Add ( i ) ; } } }