De Bruijn sequence | Set 1 | C # implementation of the above approach ; Modified DFS in which no edge is traversed twice ; Function to find a de Bruijn sequence of order n on k characters ; Clearing global variables ; Number of edges ; Driver code
using System ; using System . Collections . Generic ; class GFG { static HashSet < String > seen = new HashSet < String > ( ) ; static List < int > edges = new List < int > ( ) ; 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 ) ; } } } static String deBruijn ( int n , int k , String A ) { seen . Clear ( ) ; edges . Clear ( ) ; String startingNode = strings ( n - 1 , A [ 0 ] ) ; dfs ( startingNode , k , A ) ; String S = " " ; int l = ( int ) Math . Pow ( k , n ) ; for ( int i = 0 ; i < l ; ++ i ) S += A [ edges [ i ] ] ; S += startingNode ; return S ; } private static String strings ( int n , char charAt ) { String str = " " ; for ( int i = 0 ; i < n ; i ++ ) str += charAt ; return str ; } public static void Main ( String [ ] args ) { int n = 3 , k = 2 ; String A = "01" ; Console . Write ( deBruijn ( n , k , A ) ) ; } }