codeparrot/xlcost-text-to-code huggingface dataset:
Weird Number | Java program to check if the number is weird or not ; code to find all the factors of the number excluding the number itself ; ArrayList to store the factors ; note that this loop runs till sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the ArrayList ; Function to check if the number is abundant or not ; find the divisors using function ; sum all the factors ; check for abundant or not ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the ArrayList ; subset to check if no is semiperfect ; initialising 1 st column to true ; initialing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; if not possible to make the number by any combination of divisors ; Function to check for weird or not ; Driver Code
import java . util . * ; class GFG { static ArrayList < Integer > factors ( int n ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; v . add ( 1 ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . add ( i ) ; if ( n / i != i ) { v . add ( n / i ) ; } } } return v ; } static boolean checkAbundant ( int n ) { ArrayList < Integer > v ; int sum = 0 ; v = factors ( n ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { sum += v . get ( i ) ; } if ( sum > n ) return true ; else return false ; } static boolean checkSemiPerfect ( int n ) { ArrayList < Integer > v ; v = factors ( n ) ; Collections . sort ( v ) ; int r = v . size ( ) ; boolean subset [ ] [ ] = new boolean [ r + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= r ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j < v . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; else { subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - v . get ( i - 1 ) ] ; } } } if ( ( subset [ r ] [ n ] ) == false ) return false ; else return true ; } static boolean checkweird ( int n ) { if ( checkAbundant ( n ) == true && checkSemiPerfect ( n ) == false ) return true ; else return false ; } public static void main ( String args [ ] ) { int n = 70 ; if ( checkweird ( n ) ) System . out . println ( " Weird ▁ Number " ) ; else System . out . println ( " Not ▁ Weird ▁ Number " ) ; } }
Maximum subarray sum in an array created after repeated concatenation | Java program to print largest contiguous array sum when array is created after concatenating a small array k times . ; Returns sum of maximum sum subarray created after concatenating a [ 0. . n - 1 ] k times . ; This is where it differs from Kadane 's algorithm. We use modular  arithmetic to find next element. ; Driver program to test maxSubArraySum
import java . io . * ; class GFG { static int maxSubArraySumRepeated ( int a [ ] , int n , int k ) { int max_so_far = 0 ; int INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < n * k ; i ++ ) { max_ending_here = max_ending_here + a [ i % n ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 20 , - 30 , - 1 } ; int n = a . length ; int k = 3 ; System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + maxSubArraySumRepeated ( a , n , k ) ) ; } }
Longest Increasing Odd Even Subsequence | Java implementation to find the longest increasing odd even subsequence ; function to find the longest increasing odd even subsequence ; lioes [ i ] stores longest increasing odd even subsequence ending at arr [ i ] ; to store the length of longest increasing odd even subsequence ; Initialize LIOES values for all indexes ; Compute optimized LIOES values in bottom up manner ; Pick maximum of all LIOES values ; required maximum length ; driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int longOddEvenIncSeq ( int arr [ ] , int n ) { int [ ] lioes = new int [ n ] ; int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) lioes [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && ( arr [ i ] + arr [ j ] ) % 2 != 0 && lioes [ i ] < lioes [ j ] + 1 ) lioes [ i ] = lioes [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( maxLen < lioes [ i ] ) maxLen = lioes [ i ] ; return maxLen ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 1 , 12 , 2 , 22 , 5 , 30 , 31 , 14 , 17 , 11 } ; int n = 10 ; System . out . println ( " Longest ▁ Increasing ▁ Odd " + " ▁ Even ▁ Subsequence : ▁ " + longOddEvenIncSeq ( arr , n ) ) ; } }
Minimum and Maximum values of an expression with * and + | Java program to get maximum and minimum values of an expression ; Utility method to check whether a character is operator or not ; method prints minimum and maximum value obtainable from an expression ; store operator and numbers in different vectors ; storing last number in vector ; initializing minval and maxval 2D array ; initializing main diagonal by num values ; looping similar to matrix chain multiplication and updating both 2D arrays ; if current operator is ' + ' , updating tmp variable by addition ; if current operator is ' * ' , updating tmp variable by multiplication ; updating array values by tmp variables ; last element of first row will store the result ; Driver code to test above methods
import java . io . * ; import java . util . * ; class GFG { static boolean isOperator ( char op ) { return ( op == ' + ' op == ' * ' ) ; } static void printMinAndMaxValueOfExp ( String exp ) { Vector < Integer > num = new Vector < Integer > ( ) ; Vector < Character > opr = new Vector < Character > ( ) ; String tmp = " " ; for ( int i = 0 ; i < exp . length ( ) ; i ++ ) { if ( isOperator ( exp . charAt ( i ) ) ) { opr . add ( exp . charAt ( i ) ) ; num . add ( Integer . parseInt ( tmp ) ) ; tmp = " " ; } else { tmp += exp . charAt ( i ) ; } } num . add ( Integer . parseInt ( tmp ) ) ; int len = num . size ( ) ; int [ ] [ ] minVal = new int [ len ] [ len ] ; int [ ] [ ] maxVal = new int [ len ] [ len ] ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = 0 ; j < len ; j ++ ) { minVal [ i ] [ j ] = Integer . MAX_VALUE ; maxVal [ i ] [ j ] = 0 ; if ( i == j ) minVal [ i ] [ j ] = maxVal [ i ] [ j ] = num . get ( i ) ; } } for ( int L = 2 ; L <= len ; L ++ ) { for ( int i = 0 ; i < len - L + 1 ; i ++ ) { int j = i + L - 1 ; for ( int k = i ; k < j ; k ++ ) { int minTmp = 0 , maxTmp = 0 ; if ( opr . get ( k ) == ' + ' ) { minTmp = minVal [ i ] [ k ] + minVal [ k + 1 ] [ j ] ; maxTmp = maxVal [ i ] [ k ] + maxVal [ k + 1 ] [ j ] ; } else if ( opr . get ( k ) == ' * ' ) { minTmp = minVal [ i ] [ k ] * minVal [ k + 1 ] [ j ] ; maxTmp = maxVal [ i ] [ k ] * maxVal [ k + 1 ] [ j ] ; } if ( minTmp < minVal [ i ] [ j ] ) minVal [ i ] [ j ] = minTmp ; if ( maxTmp > maxVal [ i ] [ j ] ) maxVal [ i ] [ j ] = maxTmp ; } } } System . out . print ( " Minimum ▁ value ▁ : ▁ " + minVal [ 0 ] [ len - 1 ] + " , ▁ Maximum ▁ value ▁ : ▁ " + maxVal [ 0 ] [ len - 1 ] ) ; } public static void main ( String [ ] args ) { String expression = "1 + 2*3 + 4*5" ; printMinAndMaxValueOfExp ( expression ) ; } }
Matrix Chain Multiplication | DP | A naive recursive implementation that simply follows the above optimal substructure property ; Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; place parenthesis at different places between first and last matrix , recursively calculate count of multiplications for each parenthesis placement and return the minimum count ; Return minimum count ; Driver code
class MatrixChainMultiplication { static int MatrixChainOrder ( int p [ ] , int i , int j ) { if ( i == j ) return 0 ; int min = Integer . MAX_VALUE ; for ( int k = i ; k < j ; k ++ ) { int count = MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( count < min ) min = count ; } return min ; } public static void main ( String args [ ] ) { int arr [ ] = new int [ ] { 1 , 2 , 3 , 4 , 3 } ; int n = arr . length ; System . out . println ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " + MatrixChainOrder ( arr , 1 , n - 1 ) ) ; } }
Numbers formed by flipping common set bits in two given integers | Java program to implement the above approach ; Function to flip bits of A and B which are set in both of them ; Clear the bits of A which are set in both A and B ; Clear the bits of B which are set in both A and B ; Print updated A and B ; Driver Code
import java . util . * ; class GFG { static void flipBitsOfAandB ( int A , int B ) { A = A ^ ( A & B ) ; B = B ^ ( A & B ) ; System . out . print ( A + " ▁ " + B ) ; } public static void main ( String [ ] args ) { int A = 10 , B = 20 ; flipBitsOfAandB ( A , B ) ; } }
Sum of Hamming difference of consecutive numbers from 0 to N | Set 2 | Java program to implement the above approach ; Function to calculate and return the hamming distance between all consecutive numbers from 0 to N ; Driver code
import java . util . * ; class GFG { static int TotalHammingDistance ( int n ) { int i = 1 , sum = 0 ; while ( n / i > 0 ) { sum = sum + n / i ; i = i * 2 ; } return sum ; } public static void main ( String [ ] args ) { int N = 9 ; System . out . println ( TotalHammingDistance ( N ) ) ; } }
Sum of all divisors from 1 to N | Set 3 | Java Program to implement the above approach ; Function to find the sum of all divisors of all numbers from 1 to N ; Stores the sum ; Marks the last point of occurence with same count ; Calculate the sum ; Return the result ; Driver Code
import java . util . * ; class GFG { static final int m = 1000000007 ; static void solve ( long n ) { long s = 0 ; for ( int l = 1 ; l <= n ; ) { int r = ( int ) ( n / Math . floor ( n / l ) ) ; int x = ( ( ( r % m ) * ( ( r + 1 ) % m ) ) / 2 ) % m ; int y = ( ( ( l % m ) * ( ( l - 1 ) % m ) ) / 2 ) % m ; int p = ( int ) ( ( n / l ) % m ) ; s = ( s + ( ( ( x - y ) % m ) * p ) % m + m ) % m ; s %= m ; l = r + 1 ; } System . out . print ( ( s + m ) % m ) ; } public static void main ( String [ ] args ) { long n = 12 ; solve ( n ) ; } }
Minimize number of cuts required to break N length stick into N unit length sticks | Java program to find minimum time required to split a stick of N length into unit pieces ; Function to return the minimum time required to split stick of N into length into unit pieces ; Return the minimum unit of time required ; Driver Code
import java . lang . * ; class GFG { static int min_time_to_cut ( int N ) { if ( N == 0 ) return 0 ; return ( int ) Math . ceil ( Math . log ( N ) / Math . log ( 2 ) ) ; } public static void main ( String [ ] args ) { int N = 100 ; System . out . print ( min_time_to_cut ( N ) ) ; } }
Count of distinct pair sum between two 1 to N value Arrays | Java implementation to count of distinct pair sum between two Array with values 1 to N ; Function to find the distinct sums ; Set to store distinct sums ; Inserting every sum ; Returning distinct sums ; Driver code
import java . util . * ; class GFG { static int findDistinctSums ( int n ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { s . add ( i + j ) ; } } return s . size ( ) ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( findDistinctSums ( N ) ) ; } }
Print Triangle separated pattern | Java program to print the triangle separated pattern using star and slash character ; Function to print pattern recursively ; Base Case ; Conditions to print slash ; Condition to print forword slash ; Condition to print backward slash ; Else print ' * ' ; Recursive call for rows ; Recursive call for changing the rows ; Driver Code ; Function Call
class GFG { static int printPattern ( int i , int j , int n ) { if ( j >= n ) { return 0 ; } if ( i >= n ) { return 1 ; } if ( j == i j == n - 1 - i ) { if ( i == n - 1 - j ) { System . out . print ( " / " ) ; } else { System . out . print ( " \ \" ) ; } } else { System . out . print ( " * " ) ; } if ( printPattern ( i , j + 1 , n ) == 1 ) { return 1 ; } System . out . println ( ) ; return printPattern ( i + 1 , 0 , n ) ; } public static void main ( String [ ] args ) { int N = 9 ; printPattern ( 0 , 0 , N ) ; } }
Find starting index for every occurrence of given array B in array A using Z | Java implementation for pattern searching in an array using Z - Algorithm ; Function to calculate Z - Array ; Loop to calculate Z - Array ; Outside the Z - box ; Inside Z - box ; Helper function to merge two arrays and create a single array ; Array to store merged array ; Copying array B ; Adding a separator ; Copying array A ; Calling Z - function ; Function to help compute the Z array ; Printing indexes where array B occur ; Driver Code
import java . io . * ; import java . util . * ; class GfG { private static int [ ] zArray ( int arr [ ] ) { int z [ ] ; int n = arr . length ; z = new int [ n ] ; int r = 0 , l = 0 ; for ( int k = 1 ; k < n ; k ++ ) { if ( k > r ) { r = l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } else { int k1 = k - l ; if ( z [ k1 ] < r - k + 1 ) z [ k ] = z [ k1 ] ; else { l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } } } return z ; } private static int [ ] mergeArray ( int A [ ] , int B [ ] ) { int n = A . length ; int m = B . length ; int z [ ] ; int c [ ] = new int [ n + m + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) c [ i ] = B [ i ] ; c [ m ] = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) c [ m + i + 1 ] = A [ i ] ; z = zArray ( c ) ; return z ; } private static void findZArray ( int A [ ] , int B [ ] , int n ) { int flag = 0 ; int z [ ] ; z = mergeArray ( A , B ) ; for ( int i = 0 ; i < z . length ; i ++ ) { if ( z [ i ] == n ) { System . out . print ( ( i - n - 1 ) + " ▁ " ) ; flag = 1 ; } } if ( flag == 0 ) { System . out . println ( " Not ▁ Found " ) ; } } public static void main ( String args [ ] ) { int A [ ] = { 1 , 2 , 3 , 2 , 3 , 2 } ; int B [ ] = { 2 , 3 } ; int n = B . length ; findZArray ( A , B , n ) ; } }
Check if a string can be repeated to make another string | Java implementation of the approach ; Function to return the count of repetitions of string a to generate string b ; If b cannot be generated by repeating a ; Repeat a count number of times ; Driver code
class GfG { static int getCount ( String a , String b ) { if ( b . length ( ) % a . length ( ) != 0 ) return - 1 ; int count = b . length ( ) / a . length ( ) ; String str = " " ; for ( int i = 0 ; i < count ; i ++ ) { str = str + a ; } if ( str . equals ( b ) ) return count ; return - 1 ; } public static void main ( String [ ] args ) { String a = " geeks " ; String b = " geeksgeeks " ; System . out . println ( getCount ( a , b ) ) ; } }
Check if a string can be formed from another string using given constraints | JAVA program to Check if a given String can be formed from another String using given constraints ; Function to check if S2 can be formed of S1 ; length of Strings ; hash - table to store count ; store count of each character ; traverse and check for every character ; if the character of s2 is present in s1 ; if the character of s2 is not present in S1 , then check if previous two ASCII characters are present in S1 ; Driver Code ; Calling function to check
import java . util . * ; class GFG { static boolean check ( String S1 , String S2 ) { int n1 = S1 . length ( ) ; int n2 = S2 . length ( ) ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( mp . containsKey ( ( int ) S1 . charAt ( i ) ) ) { mp . put ( ( int ) S1 . charAt ( i ) , mp . get ( ( int ) S1 . charAt ( i ) ) + 1 ) ; } else { mp . put ( ( int ) S1 . charAt ( i ) , 1 ) ; } } for ( int i = 0 ; i < n2 ; i ++ ) { if ( mp . containsKey ( ( int ) S2 . charAt ( i ) ) ) { mp . put ( ( int ) S2 . charAt ( i ) , mp . get ( ( int ) S2 . charAt ( i ) ) - 1 ) ; } else if ( mp . containsKey ( S2 . charAt ( i ) - 1 ) && mp . containsKey ( S2 . charAt ( i ) - 2 ) ) { mp . put ( ( S2 . charAt ( i ) - 1 ) , mp . get ( S2 . charAt ( i ) - 1 ) - 1 ) ; mp . put ( ( S2 . charAt ( i ) - 2 ) , mp . get ( S2 . charAt ( i ) - 2 ) - 1 ) ; } else { return false ; } } return true ; } public static void main ( String [ ] args ) { String S1 = " abbat " ; String S2 = " cat " ; if ( check ( S1 , S2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
Count of occurrences of a "1(0 + ) 1" pattern in a string | Java program to calculate number of times the pattern occurred in given string ; Returns count of occurrences of "1(0 + ) 1" int str . ; int count = 0 ; Initialize result ; Check if encountered '1' forms a valid pattern as specified ; if 1 encountered for first time set oneSeen to 1 ; Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern ; Driver program to test above function
public class GFG { static int countPattern ( String str ) { int len = str . length ( ) ; boolean oneSeen = false ; for ( int i = 0 ; i < len ; i ++ ) { char getChar = str . charAt ( i ) ; if ( getChar == '1' && oneSeen == true ) { if ( str . charAt ( i - 1 ) == '0' ) count ++ ; } if ( getChar == '1' && oneSeen == false ) oneSeen = true ; if ( getChar != '0' && str . charAt ( i ) != '1' ) oneSeen = false ; } return count ; } public static void main ( String [ ] args ) { String str = "100001abc101" ; System . out . println ( countPattern ( str ) ) ; } }
Convert given Strings into T by replacing characters in between strings any number of times | Java program for the above approach ; Function to check if it possible to make all the strings equal to the string T ; Stores the frequency of all the strings in the array arr [ ] ; Stores the frequency of the string T ; Iterate over the characters of the string T ; Iterate in the range [ 0 , N - 1 ] ; Iterate over the characters of the string arr [ i ] ; If freqT [ i ] is 0 and freqS [ i ] is not 0 ; If freqS [ i ] is 0 and freqT [ i ] is not 0 ; If freqS [ i ] is not freqT [ i ] * N ; Otherwise , return " Yes " ; Driver Code
public class GFG { static String checkIfPossible ( int N , String [ ] arr , String T ) { int [ ] freqS = new int [ 256 ] ; int [ ] freqT = new int [ 256 ] ; for ( char ch : T . toCharArray ( ) ) { freqT [ ch - ' a ' ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { for ( char ch : arr [ i ] . toCharArray ( ) ) { freqS [ ch - ' a ' ] ++ ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( freqT [ i ] == 0 && freqS [ i ] != 0 ) { return " No " ; } else if ( freqS [ i ] == 0 && freqT [ i ] != 0 ) { return " No " ; } else if ( freqT [ i ] != 0 && freqS [ i ] != ( freqT [ i ] * N ) ) { return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { String [ ] arr = { " abc " , " abb " , " acc " } ; String T = " abc " ; int N = arr . length ; System . out . println ( checkIfPossible ( N , arr , T ) ) ; } }
Count of groups of consecutive 1 s in a given Binary String | Java program for the above approach ; Function to find the number of the groups of 1 s only in the binary string ; Stores number of groups of 1 s ; Initialization of the stack ; Traverse the string S ; If S [ i ] is '1' ; Otherwise ; If st is empty ; If st is not empty ; Return answer ; Driver code ; Input ; Function call
import java . util . Stack ; class GFG { static int groupsOfOnes ( String S , int N ) { int count = 0 ; Stack < Integer > st = new Stack < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) st . push ( 1 ) ; else { if ( ! st . empty ( ) ) { count ++ ; while ( ! st . empty ( ) ) { st . pop ( ) ; } } } } if ( ! st . empty ( ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { String S = "100110111" ; int N = S . length ( ) ; System . out . println ( groupsOfOnes ( S , N ) ) ; } }
Palindromic strings of length 3 possible by using characters of a given string | Java program for the above approach ; Function to print all palindromic strings of length 3 that can be formed using characters of string S ; Stores the count of character ; Traverse the string S ; Stores all palindromic strings ; Iterate over the charchaters over the range [ ' a ' , ' z ' ] ; If Hash [ ch ] is equal to 2 ; Iterate over the characters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; Push the s into the set st ; If Hash [ i ] is greater than or equal to 3 ; Iterate over charchaters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; If Hash [ j ] is positive ; Push s into the set st ; Iterate over the set ; Driver code
import java . util . * ; public class Main { static void generatePalindrome ( String S ) { HashMap < Character , Integer > Hash = new HashMap < > ( ) ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( Hash . containsKey ( S . charAt ( i ) ) ) Hash . put ( S . charAt ( i ) , Hash . get ( S . charAt ( i ) ) + 1 ) ; else Hash . put ( S . charAt ( i ) , 1 ) ; } TreeSet < String > st = new TreeSet < String > ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( Hash . containsKey ( i ) && Hash . get ( i ) == 2 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { String s = " " ; if ( Hash . containsKey ( j ) && i != j ) { s += i ; s += j ; s += i ; st . add ( s ) ; } } } if ( Hash . containsKey ( i ) && Hash . get ( i ) >= 3 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { String s = " " ; if ( Hash . containsKey ( j ) ) { s += i ; s += j ; s += i ; st . add ( s ) ; } } } } for ( String ans : st ) { System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { String S = " ddabdac " ; generatePalindrome ( S ) ; } }
Count occurrences of substring X before every occurrence of substring Y in a given string | Java program for the above approach ; Function to count occurrences of the string Y in the string S for every occurrence of X in S ; Stores the count of occurrences of X ; Stores the lengths of the three strings ; Traverse the string S ; If the current substring is Y , then increment the value of count by 1 ; If the current substring is X , then print the count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void countOccurrences ( String S , String X , String Y ) { int count = 0 ; int N = S . length ( ) , A = X . length ( ) ; int B = Y . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . substring ( i , Math . min ( N , i + B ) ) . equals ( Y ) ) count ++ ; if ( S . substring ( i , Math . min ( N , i + A ) ) . equals ( X ) ) System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { String S = " abcdefdefabc " ; String X = " abc " ; String Y = " def " ; countOccurrences ( S , X , Y ) ; } }
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
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 ) ; } }