codeparrot/xlcost-text-to-code huggingface dataset:
Repeat last occurrence of each alphanumeric character to their position in character family times | Java program for the above approach ; Function to encode the given string ; Variable string to store the result ; Arrays to store the last occuring index of every character in the string ; Length of the string ; Iterate over the range ; If str [ i ] is between 0 and 9 ; If str [ i ] is between a and z ; If str [ i ] is between A and Z ; Iterate over the range ; If str [ i ] is between a and z and i is the last occurence in str ; If str [ i ] is between A and Z and i is the last occurence in str ; If str [ i ] is between 0 and 9 and i is the last occurence in str ; Print the result ; Driver Code
import java . io . * ; class GFG { static void encodeString ( String str ) { String res = " " ; int small [ ] = new int [ 26 ] ; int capital [ ] = new int [ 26 ] ; int num [ ] = new int [ 10 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { small [ i ] = 0 ; capital [ i ] = 0 ; } for ( int i = 0 ; i < 10 ; i ++ ) { num [ i ] = 0 ; } int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) >= '0' && str . charAt ( i ) <= '9' ) { num [ str . charAt ( i ) - 48 ] = i ; } else if ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { small [ str . charAt ( i ) - 97 ] = i ; } else if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) { capital [ str . charAt ( i ) - 65 ] = i ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) && small [ str . charAt ( i ) - 97 ] == i ) { int occ = str . charAt ( i ) - 96 ; while ( occ -- > 0 ) { res += str . charAt ( i ) ; } } else if ( ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) && capital [ str . charAt ( i ) - 65 ] == i ) { int occ = str . charAt ( i ) - 64 ; while ( occ -- > 0 ) { res = res + str . charAt ( i ) ; } } else if ( ( str . charAt ( i ) >= '0' && str . charAt ( i ) <= '9' ) && num [ str . charAt ( i ) - 48 ] == i ) { int occ = str . charAt ( i ) - 48 ; while ( occ -- > 0 ) { res = res + str . charAt ( i ) ; } } else { res = res + str . charAt ( i ) ; } } System . out . print ( res ) ; } public static void main ( String [ ] args ) { String str = " Ea2 , ▁ 0 , ▁ E " ; encodeString ( str ) ; } }
Form minimum number of Palindromic Strings from a given string | Java Program to implement the above approach ; Function to return minimum palindromic Strings formed ; Store the length of String ; Iterate over the String and store the frequency of all charactrs A vector to store the frequency ; Store the odd frequency characters ; Iterate through all possible characters and check the parity of frequency ; store answer in a vector ; if there are no odd Frequency characters then print the String with even characters ; store the left part of the palindromic String ; reverse the right part of the String ; store the String in ans vector ; take any character from off frequency element ; repeat the above step to form left and right Strings ; reverse the right part of the String ; store the String in ans vector ; store all other odd frequency Strings ; Print the answer ; Driver Code
import java . util . * ; class GFG { static int minimumPalindromicStrings ( String S ) { int N = S . length ( ) ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ S . charAt ( i ) - ' a ' ] ++ ; } Vector < Integer > oddFreqCharacters = new Vector < Integer > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 == 1 ) { oddFreqCharacters . add ( i ) ; freq [ i ] -- ; } freq [ i ] /= 2 ; } Vector < String > ans = new Vector < String > ( ) ; if ( oddFreqCharacters . isEmpty ( ) ) { String left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += ( char ) ( i + ' a ' ) ; } } String right = left ; right = reverse ( right ) ; ans . add ( left + right ) ; } else { String middle = " " ; int c = oddFreqCharacters . lastElement ( ) ; oddFreqCharacters . remove ( oddFreqCharacters . size ( ) - 1 ) ; middle += ( char ) ( c + ' a ' ) ; String left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += ( char ) ( i + ' a ' ) ; } } String right = left ; right = reverse ( right ) ; ans . add ( left + middle + right ) ; while ( ! oddFreqCharacters . isEmpty ( ) ) { c = oddFreqCharacters . lastElement ( ) ; oddFreqCharacters . remove ( oddFreqCharacters . size ( ) - 1 ) ; middle = " " ; middle += ( char ) ( c + ' a ' ) ; ans . add ( middle ) ; } } System . out . print ( " [ " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) ) ; if ( i != ans . size ( ) - 1 ) { System . out . print ( " , ▁ " ) ; } } System . out . print ( " ] " ) ; return 0 ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; minimumPalindromicStrings ( S ) ; } }
Make Array elements equal by replacing adjacent elements with their XOR | Java program for the above approach ; Function to check if it is possible to make all the array elements equal using the given operation ; Stores the prefix XOR array ; Calculate prefix [ i ] ; Case 1 , check if the XOR of the input array is 0 ; Case 2 Iterate over all the ways to divide the array into three non empty subarrays ; XOR of Middle Block ; XOR of Left Block ; XOR of Right Block ; Not Possible ; Driver code ; Function Call
import java . io . * ; class GFG { static void possibleEqualArray ( int A [ ] , int N ) { int [ ] pref = new int [ N ] ; pref [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = pref [ i - 1 ] ^ A [ i ] ; } if ( pref [ N - 1 ] == 0 ) { System . out . println ( " YES " ) ; return ; } int cur_xor = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { cur_xor ^= A [ i ] ; for ( int j = 0 ; j < i ; j ++ ) { if ( j != 0 ) { int middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ] ; int left_xor = pref [ j - 1 ] ; int right_xor = cur_xor ; if ( left_xor == middle_xor && middle_xor == right_xor ) { System . out . println ( " YES " ) ; return ; } } } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 2 , 2 } ; int N = A . length ; possibleEqualArray ( A , N ) ; } }
Construct an array whose Prefix XOR array starting from X is an N | Java program for the above approach ; Function to print the required array ; Iteratie from 1 to N ; Print the i - th element ; Update prev_xor to i ; Driver Code ; Given Input ; Function Call
class GFG { static void GenerateArray ( int N , int X ) { int prev_xor = X ; for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( i ^ prev_xor ) ; if ( i != N ) { System . out . print ( " ▁ " ) ; } prev_xor = i ; } } public static void main ( String args [ ] ) { int N = 4 , X = 3 ; System . out . print ( " The ▁ generated ▁ array ▁ is ▁ " ) ; GenerateArray ( N , X ) ; } }
Check if elements of a Binary Matrix can be made alternating | Java program for the above approach ; Function to create the possible grids ; Function to test if any one of them matches with the given 2 - D array ; Function to print the grid , if possible ; Function to check if the grid can be made alternating or not ; Grids to store the possible grids ; Driver code
import java . io . * ; class GFG { public static void createGrid ( char [ ] [ ] grid , boolean is1 , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( is1 ) { grid [ i ] [ j ] = '0' ; is1 = false ; } else { grid [ i ] [ j ] = '1' ; is1 = true ; } } if ( M % 2 == 0 ) is1 = ! is1 ; } } public static boolean testGrid ( char [ ] [ ] testGrid , char [ ] [ ] Grid , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( Grid [ i ] [ j ] != ' * ' ) { if ( Grid [ i ] [ j ] != testGrid [ i ] [ j ] ) { return false ; } } } } return true ; } public static void printGrid ( char [ ] [ ] grid , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { System . out . print ( grid [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void findPossibleGrid ( int N , int M , char [ ] [ ] grid ) { char [ ] [ ] gridTest1 = new char [ N ] [ 1001 ] ; char [ ] [ ] gridTest2 = new char [ N ] [ 1001 ] ; createGrid ( gridTest1 , true , N , M ) ; createGrid ( gridTest2 , false , N , M ) ; if ( testGrid ( gridTest1 , grid , N , M ) ) { System . out . println ( " Yes " ) ; printGrid ( gridTest1 , N , M ) ; } else if ( testGrid ( gridTest2 , grid , N , M ) ) { System . out . println ( " Yes " ) ; printGrid ( gridTest2 , N , M ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int N = 4 , M = 4 ; char [ ] [ ] grid = { { ' * ' , ' * ' , '1' , '0' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , '0' , '1' } } ; findPossibleGrid ( N , M , grid ) ; } }
Find non | Java program for the above approach ; Function to find the possible output array ; Base case for the recursion ; If ind becomes half of the size then print the array . ; Exit the function . ; Iterate in the range . ; Put the values in the respective indices . ; Call the function to find values for other indices . ; Driver code
import java . io . * ; class GFG { static int INF64 = ( int ) 1e10 ; static int N = 200 * 1000 + 13 ; static int n ; static int arr [ ] = new int [ N ] ; static int brr [ ] = new int [ N ] ; static void brute ( int ind , int l , int r ) { if ( ind == n / 2 ) { for ( int i = 0 ; i < ( int ) n ; i ++ ) System . out . print ( brr [ i ] + " ▁ " ) ; System . exit ( 0 ) ; } for ( int i = l ; i <= arr [ ind ] / 2 ; ++ i ) if ( arr [ ind ] - i <= r ) { brr [ ind ] = i ; brr [ n - ind - 1 ] = arr [ ind ] - i ; brute ( ind + 1 , i , arr [ ind ] - i ) ; } } public static void main ( String [ ] args ) { n = 2 ; n *= 2 ; arr [ 0 ] = 5 ; arr [ 1 ] = 6 ; brute ( 0 , 0 , INF64 ) ; } }
Count of minimum numbers having K as the last digit required to obtain sum N | Java program for above approach ; Stores the smallest number that ends with digit i ( 0 , 9 ) ; Stores the minimum number of steps to create a number ending with digit i ; Initialize elements as infinity ; Minimum number ending with digit i ; Minimum steps to create a number ending with digit i ; If N < SmallestNumber then , return - 1 ; Otherwise , return answer ; Driver Code
import java . util . * ; class GFG { static int minCount ( int N , int K ) { int SmallestNumber [ ] = new int [ 10 ] ; int MinimumSteps [ ] = new int [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) { SmallestNumber [ i ] = Integer . MAX_VALUE ; MinimumSteps [ i ] = Integer . MAX_VALUE ; } for ( int i = 1 ; i <= 10 ; i ++ ) { int num = K * i ; SmallestNumber [ num % 10 ] = Math . min ( SmallestNumber [ num % 10 ] , num ) ; MinimumSteps [ num % 10 ] = Math . min ( MinimumSteps [ num % 10 ] , i ) ; } if ( N < SmallestNumber [ N % 10 ] ) { return - 1 ; } else { return MinimumSteps [ N % 10 ] ; } } public static void main ( String [ ] args ) { int N = 42 , K = 7 ; System . out . println ( minCount ( N , K ) ) ; } }
Minimum number of operations required to make an array non | Java program for the above approach ; Function to count the minimum number of steps required to make arr non - decreasing ; Stores differences ; Stores the max number ; Traverse the array arr [ ] ; Update mx ; Update val ; Stores the result ; Iterate until 2 ^ res - 1 is less than val ; Return the answer ; Driver Code ; Given input ; Function call
import java . io . * ; class GFG { static int countMinSteps ( int arr [ ] , int N ) { int val = 0 ; int mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int curr = arr [ i ] ; mx = Math . max ( mx , curr ) ; val = Math . max ( val , mx - curr ) ; } long res = 0 ; while ( ( 1 << res ) - 1 < val ) { ++ res ; } return ( int ) res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 6 , 5 } ; int N = arr . length ; System . out . println ( countMinSteps ( arr , N ) ) ; } }
Minimum XOR of at most K elements in range [ L , R ] | Java program for the above approach ; Function for K = 2 ; Function for K = 2 ; Function for K = 2 ; Function to calculate the minimum XOR of at most K elements in [ L , R ] ; Driver code ; Input ; Function call
import java . io . * ; class GFG { static int func2 ( int L , int R , int K ) { if ( R - L >= 2 ) return 1 ; return Math . min ( L , L ^ R ) ; } static int func3 ( int L , int R , int K ) { if ( ( R ^ L ) > L && ( R ^ L ) < R ) return 0 ; return func2 ( L , R , K ) ; } static int func4 ( int L , int R , int K ) { if ( R - L >= 4 ) return 0 ; int minval = L ^ ( L + 1 ) ^ ( L + 2 ) ^ ( L + 3 ) ; return Math . min ( minval , func3 ( L , R , K ) ) ; } static int minimumXor ( int L , int R , int K ) { if ( K > 4 ) return 0 ; else if ( K == 4 ) return func4 ( L , R , K ) ; else if ( K == 3 ) return func3 ( L , R , K ) ; else if ( K == 2 ) return func2 ( L , R , K ) ; else return L ; } public static void main ( String [ ] args ) { int L = 1 , R = 3 , K = 3 ; System . out . println ( minimumXor ( L , R , K ) ) ; } }
Nth term of Ruler Function Series | Java program for the above approach ; Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; System . out . println ( x ) ; } public static void main ( String [ ] args ) { int N = 8 ; findNthTerm ( N ) ; } }
Quadratic equation whose roots are reciprocal to the roots of given equation | Java program for the above approach ; Function to find the quadratic equation having reciprocal roots ; Print quadratic equation ; Driver Code ; Given coefficients ; Function call to find the quadratic equation having reciprocal roots
class GFG { static void findEquation ( int A , int B , int C ) { System . out . print ( " ( " + C + " ) " + " x ^ 2 ▁ + ( " + B + " ) x ▁ + ▁ ( " + A + " ) ▁ = ▁ 0" ) ; } public static void main ( String args [ ] ) { int A = 1 , B = - 5 , C = 6 ; findEquation ( A , B , C ) ; } }
Minimize subtraction followed by increments of adjacent elements required to make all array elements equal | Java program for the above approach ; Function to find the minimum number of moves required to make all array elements equal ; Store the total sum of the array ; Calculate total sum of the array ; If the sum is not divisible by N , then print " - 1" ; Stores the average ; Stores the count of operations ; Traverse the array arr [ ] ; Update number of moves required to make current element equal to avg ; Update the overall count ; Return the minimum operations required ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int findMinMoves ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % N != 0 ) return - 1 ; int avg = sum / N ; int total = 0 ; int needCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { needCount += ( arr [ i ] - avg ) ; total = Math . max ( Math . max ( Math . abs ( needCount ) , arr [ i ] - avg ) , total ) ; } return total ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 5 } ; int N = arr . length ; System . out . println ( findMinMoves ( arr , N ) ) ; } }
Maximum amount of money that can be collected by a player in a game of coins | Java program to implement the above approach ; Function to calculate the maximum amount collected by A ; Stores the money obtained by A ; Stores mid elements of odd sized rows ; Size of current row ; Increase money collected by A ; Add coins at even indices to the amount collected by A ; Print the amount ; Driver Code ; Function call to calculate the amount of coins collected by A
import java . util . * ; class GFG { static void find ( int N , int [ ] [ ] Arr ) { int amount = 0 ; ArrayList < Integer > mid_odd = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int siz = Arr [ i ] . length ; for ( int j = 0 ; j < siz / 2 ; j ++ ) amount = amount + Arr [ i ] [ j ] ; if ( siz % 2 == 1 ) mid_odd . add ( Arr [ i ] [ siz / 2 ] ) ; } Collections . sort ( mid_odd ) ; for ( int i = 0 ; i < mid_odd . size ( ) ; i ++ ) { if ( i % 2 == 0 ) amount = amount + mid_odd . get ( i ) ; } System . out . println ( amount ) ; } public static void main ( String [ ] args ) { int N = 2 ; int [ ] [ ] Arr = { { 5 , 2 , 3 , 4 } , { 1 , 6 } } ; find ( N , Arr ) ; } }
Check if an array can be split into K consecutive non | Java program for the above approach ; Function to check if array can be split into K consecutive and non - overlapping subarrays of length M consisting of a single distinct element ; Traverse over the range [ 0 , N - M - 1 ] ; Check if arr [ i ] is the same as arr [ i + m ] ; Increment current length t of pattern matched by 1 ; Check if t is equal to m , increment count of total repeated pattern ; Return true if length of total repeated pattern is k ; Update length of the current pattern ; Update count to 1 ; Finally return false if no pattern found ; Driver Code
import java . util . * ; class GFG { static String checkPattern ( int arr [ ] , int m , int k , int n ) { int count = 1 , t = 0 ; for ( int i = 0 ; i < n - m ; i ++ ) { if ( arr [ i ] == arr [ i + m ] ) { t ++ ; if ( t == m ) { t = 0 ; count ++ ; if ( count == k ) { return " Yes " ; } } } else { t = 0 ; count = 1 ; } } return " No " ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 1 , 3 , 3 , 3 , 3 } ; int M = 1 , K = 3 ; int N = arr . length ; System . out . print ( checkPattern ( arr , M , K , N ) ) ; } }
Move all digits to the beginning of a given string | Java program for the above approach ; Function to move all the digit to the beginning of the string ; Calculate the string length ; Stores the digits ; Stores the non - numeric character ; Traverse the string and check if there is a digit ; If character is a digit , add it to the string digits ; Otherwise , add it to the string nonNumericCharacter ; Append both the strings ; Print the string ; Driver Code ; Given String str
class GFG { static void moveAllDigitAtBeginning ( String str ) { int len = str . length ( ) ; StringBuilder digits = new StringBuilder ( ) ; StringBuilder nonNumericCharacter = new StringBuilder ( ) ; for ( char c : str . toCharArray ( ) ) { if ( c >= 48 && c <= 57 ) { digits . append ( c ) ; } else { nonNumericCharacter . append ( c ) ; } } digits . append ( nonNumericCharacter . toString ( ) ) ; System . out . print ( digits . toString ( ) + " ▁ " ) ; } public static void main ( String args [ ] ) { String str = " GeeksforGeeks123" ; moveAllDigitAtBeginning ( str ) ; } }
Move all digits to the beginning of a given string | Java program for the above approach ; Function to move all the digit at the beginning of the string ; Replace all the non - numeric characters with " ▁ " and replace all digits with " ▁ " ; Print the string ; Driver Code ; Given String str
class GFG { static void moveAllDigitAtBeginning ( String str ) { String moveAllDigit = str . replaceAll ( " \\ D + " , " " ) + str . replaceAll ( " \\ d + " , " " ) ; System . out . println ( moveAllDigit ) ; } public static void main ( String args [ ] ) { String str = " GeeksforGeeks1234" ; moveAllDigitAtBeginning ( str ) ; } }
Remove all occurrences of a word from a given string using Z | Java program for the above approach ; Function to fill the Z - array for str ; L Stores start index of window which matches with prefix of str ; R Stores end index of window which matches with prefix of str ; Iterate over the characters of str ; If i is greater thn R ; Update L and R ; If subString match with prefix ; Update R ; Update Z [ i ] ; Update R ; Update k ; if Z [ k ] is less than remaining interval ; Update Z [ i ] ; Start from R and check manually ; Update R ; Update Z [ i ] ; Update R ; Function to remove all the occurrences of word from str ; Create concatenated String " P $ T " ; Store Z array of concat ; Stores String , str by removing all the occurrences of word from str ; Stores length of word ; Traverse the array , Z [ ] ; if Z [ i + pSize + 1 ] equal to length of word ; Update i ; Driver Code
import java . util . * ; class GFG { static void getZarr ( String str , int Z [ ] ) { int n = str . length ( ) ; int k ; int L = 0 ; int R = 0 ; for ( int i = 1 ; i < n ; ++ i ) { if ( i > R ) { L = R = i ; while ( R < n && str . charAt ( R - L ) == str . charAt ( R ) ) { R ++ ; } Z [ i ] = R - L ; R -- ; } else { k = i - L ; if ( Z [ k ] < R - i + 1 ) { Z [ i ] = Z [ k ] ; } else { L = i ; while ( R < n && str . charAt ( R - L ) == str . charAt ( R ) ) { R ++ ; } Z [ i ] = R - L ; R -- ; } } } } static String goodStr ( String str , String word ) { String concat = word + " $ " + str ; int l = concat . length ( ) ; int [ ] Z = new int [ l ] ; getZarr ( concat , Z ) ; String res = " " ; int pSize = word . length ( ) ; for ( int i = 0 ; i < l ; ++ i ) { if ( i + pSize < l - 1 && Z [ i + pSize + 1 ] == pSize ) { i += pSize - 1 ; } else if ( i < str . length ( ) ) { res += str . charAt ( i ) ; } } return res ; } public static void main ( String [ ] args ) { String str = " Z - kmalgorithmkmiskmkmkmhelpfulkminkmsearching " ; String word = " km " ; System . out . print ( goodStr ( str , word ) ) ; } }
Count of substrings of a string containing another given string as a substring | Set 2 | Java program for the above approach ; Function to count the substrings of string containing another given string as a substring ; Store length of string S ; Store length of string T ; Store the required count of substrings ; Store the starting index of last occurence of T in S ; Iterate in range [ 0 , n1 - n2 ] ; Check if substring from i to i + n2 is equal to T ; Check if substring from i to i + n2 is equal to T ; Mark chk as false and break the loop ; If chk is true ; Add ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) to answer ; Update the last to i + 1 ; Print the answer ; Driver code ; Function Call
class GFG { static void findOccurrences ( String S , String T ) { int n1 = S . length ( ) ; int n2 = T . length ( ) ; int ans = 0 ; int last = 0 ; for ( int i = 0 ; i <= n1 - n2 ; i ++ ) { boolean chk = true ; for ( int j = 0 ; j < n2 ; j ++ ) { if ( T . charAt ( j ) != S . charAt ( i + j ) ) { chk = false ; break ; } } if ( chk ) { ans += ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) ; last = i + 1 ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " dabc " , T = " ab " ; findOccurrences ( S , T ) ; } }