Modified DFS in which no edge is traversed twice
function dfs ( node , k , A ) { for ( var i = 0 ; i < k ; ++ i ) { var str = node + A [ i ] ; if ( ! seen . has ( str ) ) { seen . add ( str ) ; dfs ( str . substring ( 1 ) , k , A ) ; edges . push ( i ) ; } } }