text: Program to construct DFA for Regular Expression C ( A + B ) + | Java program for the above approach ; Function to find whether the given String is Accepted by the DFA ; If n <= 1 , then print No ; To count the matched characters ; Check if the first character is C ; Traverse the rest of String ; If character is A or B , increment count by 1 ; If the first character is not C , print - 1 ; If all characters matches ; Driver Code
code: import java . util . * ; class GFG { static void DFA ( String str , int N ) { if ( N <= 1 ) { System . out . print ( " No " ) ; return ; } int count = 0 ; if ( str . charAt ( 0 ) == ' C ' ) { count ++ ; for ( int i = 1 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' A ' || str . charAt ( i ) == ' B ' ) count ++ ; else break ; } } else { System . out . print ( " No " ) ; return ; } if ( count == N ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { String str = " CAABBAAB " ; int N = str . length ( ) ; DFA ( str , N ) ; } }