codeparrot/xlcost-text-to-code huggingface dataset:
Search a Word in a 2D Grid of characters | C # program to search a word in a 2D grid ; Rows and columns in given grid ; For searching in all 8 direction ; This function searches in all 8 - direction from point ( row , col ) in grid [ , ] ; If first character of word doesn 't match  with given starting point in grid. ; Search word in all 8 directions starting from ( row , col ) ; Initialize starting point for current direction ; First character is already checked , match remaining characters ; If out of bound break ; If not matched , break ; Moving in particular direction ; If all character matched , then value of k must be equal to length of word ; Searches given word in a given matrix in all 8 directions ; Consider every point as starting point and search given word ; Driver code
using System ; class GFG { static int R , C ; static int [ ] x = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; static int [ ] y = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; static bool search2D ( char [ , ] grid , int row , int col , String word ) { if ( grid [ row , col ] != word [ 0 ] ) { return false ; } int len = word . Length ; for ( int dir = 0 ; dir < 8 ; dir ++ ) { int k , rd = row + x [ dir ] , cd = col + y [ dir ] ; for ( k = 1 ; k < len ; k ++ ) { if ( rd >= R rd < 0 cd >= C cd < 0 ) { break ; } if ( grid [ rd , cd ] != word [ k ] ) { break ; } rd += x [ dir ] ; cd += y [ dir ] ; } if ( k == len ) { return true ; } } return false ; } static void patternSearch ( char [ , ] grid , String word ) { for ( int row = 0 ; row < R ; row ++ ) { for ( int col = 0 ; col < C ; col ++ ) { if ( search2D ( grid , row , col , word ) ) { Console . WriteLine ( " pattern ▁ found ▁ at ▁ " + row + " , ▁ " + col ) ; } } } } public static void Main ( String [ ] args ) { R = 3 ; C = 13 ; char [ , ] grid = { { ' G ' , ' E ' , ' E ' , ' K ' , ' S ' , ' F ' , ' O ' , ' R ' , ' G ' , ' E ' , ' E ' , ' K ' , ' S ' } , { ' G ' , ' E ' , ' E ' , ' K ' , ' S ' , ' Q ' , ' U ' , ' I ' , ' Z ' , ' G ' , ' E ' , ' E ' , ' K ' } , { ' I ' , ' D ' , ' E ' , ' Q ' , ' A ' , ' P ' , ' R ' , ' A ' , ' C ' , ' T ' , ' I ' , ' C ' , ' E ' } } ; patternSearch ( grid , " GEEKS " ) ; Console . WriteLine ( ) ; patternSearch ( grid , " EEE " ) ; } } is by -
Boyer Moore Algorithm for Pattern Searching | C # Program for Bad Character Heuristic of Boyer Moore String Matching Algorithm ; A utility function to get maximum of two integers ; The preprocessing function for Boyer Moore 's  bad character heuristic ; Initialize all occurrences as - 1 ; Fill the actual value of last occurrence of a character ; A pattern searching function that uses Bad Character Heuristic of Boyer Moore Algorithm ; Fill the bad character array by calling the preprocessing function badCharHeuristic ( ) for given pattern ; s is shift of the pattern with respect to text ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at current shift , then index j will become - 1 after the above loop ; Shift the pattern so that the next character in text aligns with the last occurrence of it in pattern . The condition s + m < n is necessary for the case when pattern occurs at the end of text ; Shift the pattern so that the bad character in text aligns with the last occurrence of it in pattern . The max function is used to make sure that we get a positive shift . We may get a negative shift if the last occurrence of bad character in pattern is on the right side of the current character . ; Driver program to test above function
using System ; public class AWQ { static int NO_OF_CHARS = 256 ; static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static void badCharHeuristic ( char [ ] str , int size , int [ ] badchar ) { int i ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) badchar [ i ] = - 1 ; for ( i = 0 ; i < size ; i ++ ) badchar [ ( int ) str [ i ] ] = i ; } static void search ( char [ ] txt , char [ ] pat ) { int m = pat . Length ; int n = txt . Length ; int [ ] badchar = new int [ NO_OF_CHARS ] ; badCharHeuristic ( pat , m , badchar ) ; int s = 0 ; while ( s <= ( n - m ) ) { int j = m - 1 ; while ( j >= 0 && pat [ j ] == txt [ s + j ] ) j -- ; if ( j < 0 ) { Console . WriteLine ( " Patterns ▁ occur ▁ at ▁ shift ▁ = ▁ " + s ) ; s += ( s + m < n ) ? m - badchar [ txt [ s + m ] ] : 1 ; } else s += max ( 1 , j - badchar [ txt [ s + j ] ] ) ; } } public static void Main ( ) { char [ ] txt = " ABAAABCD " . ToCharArray ( ) ; char [ ] pat = " ABC " . ToCharArray ( ) ; search ( txt , pat ) ; } }
Sum of nodes at maximum depth of a Binary Tree | C # code for sum of nodes at maximum depth ; Constructor ; function to find the sum of nodes at maximum depth arguments are node and max , where max is to match the depth of node at every call to node , if max will be equal to 1 , means we are at deepest node . ; base case ; max == 1 to track the node at deepest level ; recursive call to left and right nodes ; maxDepth function to find the max depth of the tree ; base case ; either leftDepth of rightDepth is greater add 1 to include height of node at which call is ; call to function to calculate max depth ; Driver code ; Constructing tree ; call to calculate required sum
using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } public class GfG { public static int sumMaxLevelRec ( Node node , int max ) { if ( node == null ) { return 0 ; } if ( max == 1 ) { return node . data ; } return sumMaxLevelRec ( node . left , max - 1 ) + sumMaxLevelRec ( node . right , max - 1 ) ; } public static int maxDepth ( Node node ) { if ( node == null ) { return 0 ; } return 1 + Math . Max ( maxDepth ( node . left ) , maxDepth ( node . right ) ) ; } public static int sumMaxLevel ( Node root ) { int MaxDepth = maxDepth ( root ) ; return sumMaxLevelRec ( root , MaxDepth ) ; } public static void Main ( string [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; Console . WriteLine ( sumMaxLevel ( root ) ) ; } }
Sum of Manhattan distances between repetitions in a String | C # program for the above approach ; Function to find the sum of the manhattan distances between same characters in string ; Vector to store indices for each unique character of the string ; Append the position of each character in their respective vectors ; Store the required result ; Iterate over all the characters ; Calculate sum of all elements present in the vector ; Traverse the current vector ; Find suffix [ i + 1 ] ; Adding distance of all pairs whose first element is i and second element belongs to [ i + 1 , n ] ; Print the result ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void SumofDistances ( string s ) { List < int > [ ] v = new List < int > [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) v [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { v [ ( int ) s [ i ] - 97 ] . Add ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < v [ i ] . Count ; j ++ ) { sum += v [ i ] [ j ] ; } for ( int j = 0 ; j < v [ i ] . Count ; j ++ ) { sum -= v [ i ] [ j ] ; ans += ( sum - ( v [ i ] . Count - 1 - j ) * ( v [ i ] [ j ] ) ) ; } } Console . Write ( ans ) ; } public static void Main ( ) { string s = " ababa " ; SumofDistances ( s ) ; } }
Minimum prefixes required to be flipped to convert a Binary String to another | C # program for the above approach ; Function to count flips required to make Strings A and B equal ; Stores the count of the number of operations ; Stores the length of the chosen prefixes ; Stores if operations are performed even or odd times ; Traverse the given String ; If current characters in the two Strings are unequal ; If A [ i ] is not flipped ; Increment count of operations ; Insert the length of the chosen prefix ; Update invert to true ; If A [ i ] is flipped ; Increment count of operations ; Insert length of the chosen prefix ; Update invert to false ; Print the number of operations required ; Print the chosen prefix length in each operation ; Driver Code ; Given binary Strings
using System ; using System . Collections . Generic ; class GFG { static void findOperations ( String A , String B , int N ) { int operations = 0 ; List < int > ops = new List < int > ( ) ; bool invert = false ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { if ( ! invert ) { operations ++ ; ops . Add ( i + 1 ) ; invert = true ; } } else { if ( invert ) { operations ++ ; ops . Add ( i + 1 ) ; invert = false ; } } } Console . Write ( operations + " STRNEWLINE " ) ; if ( operations != 0 ) { foreach ( int x in ops ) Console . Write ( x + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String A = "001" , B = "000" ; int N = A . Length ; findOperations ( A , B , N ) ; } }
Maximize cost of forming a set of words using given set of characters | C # program for the above approach ; Function to find the maximum cost of any valid set of words formed by using the given letters ; Stores frequency of characters ; Find the maximum cost ; Utility function to find maximum cost of generating any possible subsets of strings ; Base Case ; Stores the current cost ; Stores the cost of by the current word ; Create a copy of the letterCounts array ; Traverse the current word ; Store the current index & check if its frequency is 0 or not ; If true , then update wordScore to - 1 ; Otherwise , add the cost of the current index to wordScore ; Decrease its frequency ; If wordScore > 0 , then recursively call for next index ; Find the cost by not including current word ; Return the maximum score ; Driver Code ; Given arrays ; Function Call
using System ; using System . Linq ; class GFG { public static int maxScoreWords ( string [ ] words , char [ ] letters , int [ ] score ) { int [ ] letterCounts = new int [ 26 ] ; foreach ( char letter in letters ) letterCounts [ letter - ' a ' ] ++ ; return helper ( words , 0 , letterCounts , score ) ; } public static int helper ( string [ ] words , int start , int [ ] letterCounts , int [ ] score ) { if ( start == words . Length ) return 0 ; int currScore = 0 ; int wordScore = 0 ; int [ ] nextCounts = letterCounts . ToArray ( ) ; for ( int i = 0 ; i < words [ start ] . Length ; ++ i ) { int idx = words [ start ] [ i ] - ' a ' ; if ( nextCounts [ idx ] == 0 ) { wordScore = - 1 ; break ; } wordScore += score [ idx ] ; nextCounts [ idx ] -- ; } if ( wordScore > 0 ) currScore = helper ( words , start + 1 , nextCounts , score ) + wordScore ; currScore = Math . Max ( currScore , helper ( words , start + 1 , letterCounts , score ) ) ; return currScore ; } public static void Main ( string [ ] args ) { string [ ] words = { " dog " , " cat " , " dad " , " good " } ; char [ ] letters = { ' a ' , ' a ' , ' c ' , ' d ' , ' d ' , ' d ' , ' g ' , ' o ' , ' o ' } ; int [ ] score = { 1 , 0 , 9 , 5 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; Console . WriteLine ( maxScoreWords ( words , letters , score ) ) ; } }
Modify string by sorting characters after removal of characters whose frequency is not equal to power of 2 | C # program for the above approach ; Function to remove all the characters from a string that whose frequencies are not equal to a perfect power of 2 ; Stores the frequency of each character in S ; Iterate over characters of string ; Update frequency of current character in the array freq [ ] ; Traverse the array freq [ ] ; Check if the i - th letter is absent from string S ; Calculate log of frequency of the current character in the string S ; Calculate power of 2 of lg ; Check if freq [ i ] is a power of 2 ; Print letter i + ' a ' freq [ i ] times ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void countFrequency ( string S , int N ) { int [ ] freq = new int [ 26 ] ; Array . Clear ( freq , 0 , freq . Length ) ; for ( int i = 0 ; i < N ; i ++ ) { freq [ ( int ) S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; int lg = ( int ) Math . Log ( ( double ) freq [ i ] , 2.0 ) ; int a = ( int ) Math . Pow ( 2 , lg ) ; if ( a == freq [ i ] ) { while ( freq [ i ] > 0 ) { freq [ i ] -= 1 ; Console . Write ( ( char ) ( i + ' a ' ) ) ; } } } } public static void Main ( ) { string S = " aaacbb " ; int N = S . Length ; countFrequency ( S , N ) ; } }
Longest Common Subsequence ( LCS ) by repeatedly swapping characters of a string with characters of another string | C # program for the above approach ; Function to find the length of LCS possible by swapping any character of a string with that of another string ; Store the size of the strings ; Stores frequency of characters ; Iterate over characters of the string A ; Update frequency of character A [ i ] ; Iterate over characters of the string B ; Update frequency of character B [ i ] ; Store the count of all pairs of similar characters ; Traverse the array freq [ ] ; Update cnt ; Print the minimum of cnt , N and M ; Driver Code ; Given strings
using System ; class GFG { static void lcsBySwapping ( string A , string B ) { int N = A . Length ; int M = B . Length ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ A [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < M ; i ++ ) { freq [ B [ i ] - ' a ' ] += 1 ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { cnt += freq [ i ] / 2 ; } Console . WriteLine ( Math . Min ( cnt , Math . Min ( N , M ) ) ) ; } public static void Main ( string [ ] args ) { string A = " abdeff " ; string B = " abbet " ; lcsBySwapping ( A , B ) ; } }
Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | C # program for the above approach ; Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void maximumLengthSubsequence ( String str ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; foreach ( char ch in str . ToCharArray ( ) ) { if ( mp . ContainsKey ( ch ) ) mp [ ch ] = mp [ ch ] + 1 ; else mp . Add ( ch , 1 ) ; } int ans = 0 ; foreach ( char ch in str . ToCharArray ( ) ) { if ( mp . ContainsKey ( ( char ) ( ch - 1 ) ) ) { int curr_max = mp [ ch ] + mp [ ( char ) ( ch - 1 ) ] ; ans = Math . Max ( ans , curr_max ) ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { String S = " acbbebcg " ; maximumLengthSubsequence ( S ) ; } }
Minimum increments by 1 or K required to convert a string into another given string | C # program for the above approach ; Function to count minimum increments by 1 or K required to convert X to Y ; Convert String to character array ; Traverse the string X ; Case 1 ; Case 2 ; Add the difference / K to the count ; Add the difference % K to the count ; Case 3 ; Add the difference / K to the count ; Add the difference % K to the count ; Print the answer ; Driver code
using System ; class GFG { static void countOperations ( string X , string Y , int K ) { char [ ] ch1 = X . ToCharArray ( ) ; char [ ] ch2 = Y . ToCharArray ( ) ; int count = 0 ; for ( int i = 0 ; i < X . Length ; i ++ ) { int c = 0 ; if ( ch1 [ i ] == ch2 [ i ] ) continue ; else if ( ch1 [ i ] < ch2 [ i ] ) { if ( ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) >= K ) { c = ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) / K ; } c += ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) % K ; } else { int t = 90 - ( int ) ch1 [ i ] ; t += ( int ) ch2 [ i ] - 65 + 1 ; if ( t >= K ) c = t / K ; c += ( t % K ) ; } count += c ; } Console . WriteLine ( count ) ; } static void Main ( ) { string X = " ABCT " , Y = " PBDI " ; int K = 6 ; countOperations ( X , Y , K ) ; } }
Minimum number of swaps required such that a given substring consists of exactly K 1 s | C # program to implement the above approach ; Function to find the minimum number of swaps required such that the substring { s [ l ] , . . , s [ r ] } consists of exactly k 1 s ; Store the size of the string ; Store the total number of 1 s and 0 s in the entire string ; Traverse the string S to find the frequency of 1 and 0 ; Store the number of 1 s and 0 s in the substring s [ l , r ] ; Traverse the substring S [ l , r ] to find the frequency of 1 s and 0 s in it ; Store the count of 1 s and 0 s outside substring s [ l , r ] ; Check if the sum of the substring is at most K ; Store number of 1 s required ; Check if there are enough 1 s remaining to be swapped ; If the count of 1 s in the substring exceeds k ; Store the number of 0 s required ; Check if there are enough 0 s remaining to be swapped ; In all other cases , print - 1 ; Driver Code
using System ; class GFG { static int minimumSwaps ( string s , int l , int r , int k ) { int n = s . Length ; int tot_ones = 0 , tot_zeros = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) tot_ones ++ ; else tot_zeros ++ ; } int ones = 0 , zeros = 0 , sum = 0 ; for ( int i = l - 1 ; i < r ; i ++ ) { if ( s [ i ] == '1' ) { ones ++ ; sum ++ ; } else zeros ++ ; } int rem_ones = tot_ones - ones ; int rem_zeros = tot_zeros - zeros ; if ( k >= sum ) { int rem = k - sum ; if ( zeros >= rem && rem_ones >= rem ) return rem ; } else if ( k < sum ) { int rem = sum - k ; if ( ones >= rem && rem_zeros >= rem ) return rem ; } return - 1 ; } public static void Main ( String [ ] args ) { string S = "110011111000101" ; int L = 5 , R = 8 , K = 2 ; Console . Write ( minimumSwaps ( S , L , R , K ) ) ; } }
Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | C # Program to implement the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans [ ] or not ; If the number exists ; Driver Code
using System ; class GFG { static int N = 200000 ; static long [ ] Ans = new long [ 200000 ] ; static int index = 0 ; static void populateNumber ( ) { Ans [ index ++ ] = ( 2 ) ; Ans [ index ++ ] = ( 5 ) ; long x = 5 ; long inf = 1000000000001L ; while ( x < inf ) { x *= 2 ; Ans [ index ++ ] = ( x ) ; x = x * 2 + 1 ; Ans [ index ++ ] = ( x ) ; } } static void checkString ( int N ) { populateNumber ( ) ; for ( int i = 0 ; i < index ; i ++ ) { if ( Ans [ i ] == N ) { Console . WriteLine ( " YES " ) ; return ; } } Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { N = 5 ; checkString ( N ) ; } }
Minimize length by removing subsequences forming valid parenthesis from a given string | C # program for the above approach ; Function to find the minimum count of remaining characters left into the string by removing the valid subsequences ; Length of the string ; Stores opening parenthesis ' ( ' of the given string ; Stores square parenthesis ' [ ' of the given string ; Stores count of opening parenthesis ' ( ' in valid subsequences ; Stores count of opening parenthesis ' [ ' in valid subsequences ; Iterate over each characters of S ; If current character is ' [ ' ; insert into stack ; If i is equal to ' ] ' ; If stack is not empty and top element of stack is ' [ ' ; Remove top element from stack ; Update squareCount ; If current character is ' ( ' ; Insert into stack ; If i is equal to ' ) ' ; If stack is not empty and top element of stack is ' ( ' ; Remove top element from stack ; Update roundCount ; Print the minimum number of remaining characters left into S ; Driver code ; input string ; function call
using System ; using System . Collections . Generic ; class GFG { public static void deleteSubseq ( String s ) { int N = s . Length ; Stack < char > roundStk = new Stack < char > ( ) ; Stack < char > squareStk = new Stack < char > ( ) ; int roundCount = 0 ; int squareCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' [ ' ) { squareStk . Push ( s [ i ] ) ; } else if ( s [ i ] == ' ] ' ) { if ( squareStk . Count != 0 && squareStk . Peek ( ) == ' [ ' ) { squareStk . Pop ( ) ; squareCount += 1 ; } } else if ( s [ i ] == ' ( ' ) { roundStk . Push ( s [ i ] ) ; } else { if ( roundStk . Count != 0 && squareStk . Peek ( ) == ' ( ' ) { squareStk . Pop ( ) ; roundCount += 1 ; } } } Console . WriteLine ( N - ( 2 * squareCount + 2 * roundCount ) ) ; } public static void Main ( String [ ] args ) { String s = " [ ] ] ) ( [ " ; deleteSubseq ( s ) ; } }
Count minimum substring removals required to reduce string to a single distinct character | C # program for the above approach ; Function to find minimum removals required to convert given string to single distinct characters only ; Unordered map to store positions of characters X , Y and Z ; Update indices of X , Y , Z ; ; Stores the count of minimum removals ; Traverse the Map ; Count the number of removals required for current character ; Update the answer ; Print the answer ; Driver Code ; Given string ; Size of string ; Function call
using System ; using System . Collections . Generic ; class GFG { static void minimumOperations ( string s , int n ) { Dictionary < char , List < int > > mp = new Dictionary < char , List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( s [ i ] ) ) { mp [ s [ i ] ] . Add ( i ) ; } else { mp [ s [ i ] ] = new List < int > ( ) ; mp [ s [ i ] ] . Add ( i ) ; } } int ans = Int32 . MaxValue ; foreach ( KeyValuePair < char , List < int > > x in mp ) { int curr = 0 ; int prev = 0 ; bool first = true ; foreach ( int index in ( x . Value ) ) { if ( first ) { if ( index > 0 ) { curr ++ ; } prev = index ; first = false ; } else { if ( index != prev + 1 ) { curr ++ ; } prev = index ; } } if ( prev != n - 1 ) { curr ++ ; } ans = Math . Min ( ans , curr ) ; } Console . Write ( ans ) ; } static void Main ( ) { string s = " YYXYZYXYZXY " ; int N = s . Length ; minimumOperations ( s , N ) ; } }
Program to construct DFA accepting odd number of 0 s and odd number of 1 s | C # program for the above approach ; Function to check whether the given string is accepted by DFA or not ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Driver Code ; Given string ; Function Call
using System ; class GFG { static void checkValidDFA ( string s ) { int final_state = 0 ; int previous_state = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ( s [ i ] == '0' && previous_state == 0 ) || ( s [ i ] == '1' && previous_state == 3 ) ) { final_state = 1 ; } else if ( ( s [ i ] == '0' && previous_state == 3 ) || ( s [ i ] == '1' && previous_state == 0 ) ) { final_state = 2 ; } else if ( ( s [ i ] == '0' && previous_state == 1 ) || ( s [ i ] == '1' && previous_state == 2 ) ) { final_state = 0 ; } else if ( ( s [ i ] == '0' && previous_state == 2 ) || ( s [ i ] == '1' && previous_state == 1 ) ) { final_state = 3 ; } previous_state = final_state ; } if ( final_state == 3 ) { Console . WriteLine ( " Accepted " ) ; } else { Console . WriteLine ( " Not ▁ Accepted " ) ; } } public static void Main ( ) { string s = "010011" ; checkValidDFA ( s ) ; } }