codeparrot/xlcost-text-to-code huggingface dataset:
Bitwise XOR of all odd numbers from a given range | Java program for the above approach ; Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Print the answer ; If number is odd ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static int findXOR ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static void findOddXOR ( int n ) { if ( n % 2 == 0 ) System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ) ; else System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ) ; } public static void main ( String [ ] args ) { int N = 11 ; findOddXOR ( N ) ; } }
Smallest number greater than or equal to N which is divisible by its non | Java program for the above approach ; Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver code ; Function Call
import java . util . * ; class GFG { static void findSmallestNumber ( int n ) { for ( int i = n ; i <= ( n + 2520 ) ; ++ i ) { int possible = 1 ; int temp = i ; while ( temp != 0 ) { if ( temp % 10 != 0 ) { int digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp /= 10 ; } if ( possible == 1 ) { System . out . println ( i ) ; return ; } } } public static void main ( String [ ] args ) { int N = 31 ; findSmallestNumber ( N ) ; } }
Print path from a node to root of given Complete Binary Tree | Java program for the above approach ; Function to print the path from node to root ; Iterate until root is reached ; Print the value of the current node ; Move to parent of the current node ; Driver Code
import java . util . * ; class GFG { static void path_to_root ( int node ) { while ( node >= 1 ) { System . out . print ( node + " ▁ " ) ; node /= 2 ; } } public static void main ( String [ ] args ) { int N = 7 ; path_to_root ( N ) ; } }
Smallest number whose sum of digits is N and every digit occurring at most K times | Java program for the above approach ; Function to find the smallest number whose sum of digits is N and each digit occurring at most K times ; Maximum sum possible using each digit at most K times ; Append smallest number into the resultant string ; Iterate over the range [ 9 , 1 ] ; If the count of the digit is K , then update count and check for the next digit ; If N is greater than current digit ; Subtract i from N ; Insert i as a digit ; Insert remaining N as a digit ; Increment count ; Reverse the string ; Print the answer ; Driver Code ; Function Call
import java . io . * ; class GFG { static void findSmallestNumberPossible ( int N , int K ) { if ( N > 45 * K ) { System . out . print ( " - 1" ) ; return ; } StringBuilder res = new StringBuilder ( ) ; int count = 0 ; for ( int i = 9 ; i >= 1 { if ( count == K ) { i -- ; count = 0 ; } if ( N > i ) { N -= i ; res . append ( ( char ) ( '0' + i ) ) ; } else { res . append ( ( char ) ( '0' + N ) ) ; N = 0 ; break ; } count ++ ; } res . reverse ( ) ; System . out . print ( res . toString ( ) ) ; } public static void main ( String [ ] args ) { int N = 25 , K = 3 ; findSmallestNumberPossible ( N , K ) ; } }
Minimum sum of values subtracted from array elements to make all array elements equal | Java program for the above approach ; Function to find the sum of values removed to make all array elements equal ; Stores the minimum of the array ; Stores required sum ; Traverse the array ; Add the value subtracted from the current element ; Return the total sum ; Driver Code ; Function Call
import java . util . Arrays ; class GFG { static int minValue ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int minimum = arr [ 0 ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] - minimum ) ; } return sum ; } static public void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . length ; System . out . println ( minValue ( arr , N ) ) ; } }
Smallest positive number made up of non | Java program for the above approach ; Function to find smallest positive number made up of non - repeating digits whose sum of its digits is equal to n ; No such number exists ; Stores the required answer ; Store the digit at unit 's place ; Iterate until n > digit ; Push digit at the start of res ; Decrement n by digit ; Decrement digit by 1 ; Push the remaining number as the starting digit ; Print the required number ; Driver Code ; Function Call
import java . util . * ; class GFG { static void result ( int n ) { if ( n > 45 ) { System . out . print ( - 1 ) ; return ; } String res = " " ; int digit = 9 ; while ( n > digit ) { res = ( char ) ( '0' + digit ) + res ; n -= digit ; digit -= 1 ; } if ( n > 0 ) { res = ( char ) ( '0' + n ) + res ; } System . out . print ( res ) ; } public static void main ( String [ ] args ) { int N = 19 ; result ( N ) ; } }
Count pair of integers having even sum | Java program for the above approach ; Function to count even pairs ; Stores count of pairs having even sum ; Stores count of even numbers up to N ; Stores count of odd numbers up to N ; Stores count of even numbers up to M ; Stores count of odd numbers up to M ; Return the count ; Driver Code
import java . io . * ; class GFG { public static int countEvenPairs ( int N , int M ) { int count = 0 ; int nEven = ( int ) Math . floor ( ( double ) N / 2 ) ; int nOdd = ( int ) Math . ceil ( ( double ) N / 2 ) ; int mEven = ( int ) Math . floor ( ( double ) M / 2 ) ; int mOdd = ( int ) Math . ceil ( ( double ) M / 2 ) ; count = nEven * mEven + nOdd * mOdd ; return count ; } public static void main ( String [ ] args ) { int N = 4 ; int M = 6 ; System . out . print ( countEvenPairs ( N , M ) ) ; } }
Generate an N | Java program to implement the above approach ; Function to generate a String of length N having longest palindromic subString of length K ; Fill first K characters with ' a ' ; Stores a non - palindromic sequence to be repeated for N - k slots ; Print N - k remaining characters ; Driver Code ; Given N and K
import java . util . * ; class GFG { static void String_palindrome ( int N , int K ) { for ( int i = 0 ; i < K ; i ++ ) System . out . print ( " a " ) ; String s = " bcd " ; for ( int i = 0 ; i < N - K ; i ++ ) System . out . print ( s . charAt ( i % 3 ) ) ; } public static void main ( String [ ] args ) { int N = 5 , K = 3 ; String_palindrome ( N , K ) ; } }
Last remaining character after repeated removal of the first character and flipping of characters of a Binary String | Java program to implement the above approach ; Function to find the last removed character from the String ; Stores length of the String ; Base Case : ; If the second last character is '0' ; If the second last character is '1' ; Driver Code
import java . util . * ; class GFG { static char lastRemovedCharacter ( char [ ] str ) { int n = str . length ; if ( n == 1 ) return str [ 0 ] ; if ( str [ n - 2 ] == '0' ) { return ( char ) ( '1' - str [ n - 1 ] + '0' ) ; } else return str [ n - 1 ] ; } public static void main ( String [ ] args ) { String str = "10010" ; System . out . print ( lastRemovedCharacter ( str . toCharArray ( ) ) ) ; } }
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs | Java program to implement the above approach ; Function to find the triplet with given Bitwise XOR and Bitwise AND values of all possible pairs of the triplet ; Stores values of a triplet ; Stores a + b ; Stores a + c ; Stores b + c ; Calculate aSUMb ; Calculate aSUMc ; Calculate bSUMc ; Calculate a ; Calculate b ; Calculate c ; Print a ; Print b ; Print c ; Driver Code
class GFG { static void findNumbers ( int aXORb , int aANDb , int aXORc , int aANDc , int bXORc , int bANDc ) { int a , b , c ; int aSUMb ; int aSUMc ; int bSUMc ; aSUMb = aXORb + aANDb * 2 ; aSUMc = aXORc + aANDc * 2 ; bSUMc = bXORc + bANDc * 2 ; a = ( aSUMb - bSUMc + aSUMc ) / 2 ; b = aSUMb - a ; c = aSUMc - a ; System . out . print ( " a ▁ = ▁ " + a ) ; System . out . print ( " , ▁ b ▁ = ▁ " + b ) ; System . out . print ( " , ▁ c ▁ = ▁ " + c ) ; } public static void main ( String [ ] args ) { int aXORb = 30 , aANDb = 0 , aXORc = 20 , aANDc = 10 , bXORc = 10 , bANDc = 20 ; findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) ; } }
Find N distinct numbers whose Bitwise XOR is equal to K | Java program for the above approach ; Function to find N integers having Bitwise XOR equal to K ; Base Cases ; Assign values to P and Q ; Stores Bitwise XOR of the first ( N - 3 ) elements ; Print the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call
import java . util . * ; class GFG { static void findArray ( int N , int K ) { if ( N == 1 ) { System . out . print ( K + " ▁ " ) ; return ; } if ( N == 2 ) { System . out . print ( 0 + " ▁ " + K ) ; return ; } int P = N - 2 ; int Q = N - 1 ; int VAL = 0 ; for ( int i = 1 ; i <= ( N - 3 ) ; i ++ ) { System . out . print ( i + " ▁ " ) ; VAL ^= i ; } if ( VAL == K ) { System . out . print ( P + " ▁ " + Q + " ▁ " + ( P ^ Q ) ) ; } else { System . out . print ( 0 + " ▁ " + P + " ▁ " + ( P ^ K ^ VAL ) ) ; } } public static void main ( String [ ] args ) { int N = 4 , X = 6 ; findArray ( N , X ) ; } }
Flip consecutive set bits starting from LSB of a given number | Java program for the above approach ; Function to find the number after converting 1 s from end to 0 s ; Count of 1 s ; AND operation of N and 1 ; Left shift N by count ; Driver Code ; Function Call
class GFG { static int findNumber ( int N ) { int count = 0 ; while ( ( N & 1 ) == 1 ) { N = N >> 1 ; count ++ ; } return N << count ; } public static void main ( String [ ] args ) { int N = 39 ; System . out . println ( findNumber ( N ) ) ; } }
Flip consecutive set bits starting from LSB of a given number | Java program for the above approach ; Function to find the number after converting 1 s from end to 0 s ; Return the logical AND of N and ( N + 1 ) ; Driver Code ; Function Call
import java . util . * ; class GFG { static int findNumber ( int N ) { return N & ( N + 1 ) ; } public static void main ( String [ ] args ) { int N = 39 ; System . out . print ( findNumber ( N ) ) ; } }
Count of substrings of a string containing another given string as a substring | Java program for the above approach ; Function to store all subStrings of S ; Stores the subStrings of S ; Pick start point in outer loop and lengths of different Strings for a given starting point ; Return the array containing subStrings of S ; Function to check if a String is present in another String ; Check if target is in the String str or not ; Function to count the subString of S containing T in it as subString ; Store all subStrings of S in the array v [ ] ; Store required count of subStrings ; Iterate through all the subStrings of S ; If String T is present in the current subString , then increment the ans ; Print the answer ; Driver code ; Function Call
import java . util . * ; class GFG { static Vector < String > subString ( String s , int n ) { Vector < String > v = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { String find = s . substring ( i , i + len ) ; v . add ( find ) ; } } return v ; } static int IsPresent ( String str , String target ) { if ( str . contains ( target ) ) { return 1 ; } return - 1 ; } static void countSubStrings ( String S , String T ) { Vector < String > v = subString ( S , S . length ( ) ) ; int ans = 0 ; for ( String it : v ) { if ( IsPresent ( it , T ) != - 1 ) { ans ++ ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S = " dabc " ; String T = " ab " ; countSubStrings ( S , T ) ; } }
Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | Java program for the above approach ; Function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; Stores count of total operations ; Stores count of consecutive unequal elements ; Loop to run on odd positions ; Incrementing the global counter ; Change count to 0 ; If all last elements are equal ; Loop to run on even positions ; Incrementing the global counter ; Change count to 0 ; Print the minimum operations ; Driver Code ; Function Call
import java . util . * ; class GFG { static void minOperation ( int X [ ] , int Y [ ] , int n ) { int C = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; count = 0 ; for ( int i = 0 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; System . out . print ( C ) ; } public static void main ( String [ ] args ) { int X [ ] = { 1 , 0 , 0 , 0 , 0 , 1 } ; int Y [ ] = { 1 , 1 , 0 , 1 , 1 , 1 } ; int N = X . length ; minOperation ( X , Y , N ) ; } }
Minimum subarray reversals required to make given binary array alternating | Java program to implement the above approach ; Function to count minimum subarray reversal operations required to make array alternating ; Stores minimum count of operations required to make array alternating ; Traverse the array ; If arr [ i ] is greater than arr [ i + 1 ] ; Update cntOp ; Driver Code
import java . util . * ; class GFG { static int minimumcntOperationReq ( int arr [ ] , int N ) { int cntOp = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { cntOp ++ ; } } return ( cntOp + 1 ) / 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 } ; int N = arr . length ; System . out . print ( minimumcntOperationReq ( arr , N ) ) ; } }
Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD | Java program for the above approach ; Function to check if array arr [ ] can be obtained from first N natural numbers or not ; Driver code ; Function Call
class GFG { static void isSequenceValid ( int [ ] B , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( i + 1 ) % B [ i ] != 0 ) { System . out . print ( " No " ) ; return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int N = 4 ; int [ ] arr = { 1 , 2 , 3 , 2 } ; isSequenceValid ( arr , N ) ; } }
Construct an array of first N natural numbers such that every adjacent pair is coprime | Java program to implement the above approach ; Function to construct an arrary with adjacent elements as co - prime numbers ; Iterate over the range [ 1 , N ] ; Print i ; Driver Code
class GFG { static void ConstArrayAdjacentCoprime ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 6 ; ConstArrayAdjacentCoprime ( N ) ; } }
Maximize count of rows consisting of equal elements by flipping columns of a Matrix | Java program to implement ; Function to find the maximum number of rows containing all equal elements ; Store each row of the matrix ; Traverse each row of the matrix ; Update frequency of current row ; Stores maximum count of rows containing all equal elements ; Traverse each row of the matrix ; Stores 1 's complement  of the current row ; Traverse current row of the given matrix ; Stores 1 s complement of mat [ i ] [ j ] ; Update cntMaxRows ; Driver code ; Stores count of rows ; Stores count of columns
import java . util . * ; class GFG { static int maxEqrows ( Vector < Vector < Integer > > mat , int N , int M ) { HashMap < Vector < Integer > , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( mat . get ( i ) ) ) { mp . put ( mat . get ( i ) , mp . get ( mat . get ( i ) ) + 1 ) ; } else { mp . put ( mat . get ( i ) , 1 ) ; } } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { Vector < Integer > onesCompRow = new Vector < Integer > ( ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . add ( 0 ) ; } for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . set ( j , mat . get ( i ) . get ( j ) ^ 1 ) ; } if ( ! mp . containsKey ( mat . get ( i ) ) ) { cntMaxRows = Math . max ( cntMaxRows , mp . get ( onesCompRow ) ) ; } else if ( ! mp . containsKey ( onesCompRow ) ) { cntMaxRows = Math . max ( cntMaxRows , mp . get ( mat . get ( i ) ) ) ; } else { cntMaxRows = Math . max ( cntMaxRows , mp . get ( mat . get ( i ) ) + mp . get ( onesCompRow ) ) ; } } return cntMaxRows ; } public static void main ( String [ ] args ) { Vector < Vector < Integer > > mat = new Vector < Vector < Integer > > ( ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 1 ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 0 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 0 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 0 ) ; int N = mat . size ( ) ; int M = mat . get ( 0 ) . size ( ) ; System . out . println ( maxEqrows ( mat , N , M ) ) ; } }
Minimum cost to convert one given string to another using swap , insert or delete operations | Java program for the above approach ; Function to find the minimum cost to convert string a to b ; Stores the frequency of string a and b respectively ; Store the frequencies of characters in a ; Store the frequencies of characters in b ; Minimum cost to convert A to B ; Find the minimum cost ; Print the minimum cost ; Driver Code ; Function Call
import java . util . * ; class GFG { public static void minimumCost ( String a , String b ) { int fre1 [ ] = new int [ 256 ] ; int fre2 [ ] = new int [ 256 ] ; for ( char c : a . toCharArray ( ) ) fre1 [ ( int ) ( c ) ] ++ ; for ( char c : b . toCharArray ( ) ) fre2 [ ( int ) ( c ) ] ++ ; int mincost = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { mincost += Math . abs ( fre1 [ i ] - fre2 [ i ] ) ; } System . out . println ( mincost ) ; } public static void main ( String [ ] args ) { String A = "1AB + - " , B = " cc " ; minimumCost ( A , B ) ; } }
Minimum cost required to move all elements to the same position | Java program to implement the above approach ; Function to find the minimum cost required to place all elements in the same position ; Stores the count of even and odd elements ; Traverse the array arr [ ] ; Count even elements ; Count odd elements ; Print the minimum count ; Driver Code ; Given array ; Function Call
import java . io . * ; class GFG { public void minCost ( int [ ] arr ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } System . out . print ( Math . min ( even , odd ) ) ; } public static void main ( String [ ] args ) { GFG obj = new GFG ( ) ; int arr [ ] = { 1 , 2 , 3 } ; obj . minCost ( arr ) ; } }
Maximize array sum by alternating the signs of adjacent elements | Java program to implement the above approach ; Function to find the maximum sum by alternating the signs of adjacent elements of the array ; Stores count of negative elements in the array ; Stores maximum sum by alternating the signs of adjacent elements ; Stores smallest absolute value of array elements ; Stores sum of absolute value of array elements ; Traverse the array ; If arr [ i ] is a negative number ; Update cntNeg ; Update sum ; Update SmValue ; Update MaxAltSum ; If cntNeg is an odd number ; Update MaxAltSum ; Driver Code
import java . util . * ; class GFG { static int findMaxSumByAlternatingSign ( int arr [ ] , int N ) { int cntNeg = 0 ; int MaxAltSum = 0 ; int SmValue = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cntNeg += 1 ; } sum += Math . abs ( arr [ i ] ) ; SmValue = Math . min ( SmValue , Math . abs ( arr [ i ] ) ) ; } MaxAltSum = sum ; if ( cntNeg % 2 == 1 ) { MaxAltSum -= 2 * SmValue ; } return MaxAltSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , - 2 , - 4 , 5 } ; int N = arr . length ; System . out . print ( findMaxSumByAlternatingSign ( arr , N ) ) ; } }
Construct array with sum of product of same indexed elements in the given array equal to zero | Java program to implement the above approach ; Function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; Stores sum of same indexed array elements of arr and new array ; Traverse the array ; If i is an even number ; Insert arr [ i + 1 ] into the new array newArr [ ] ; Insert - arr [ i - 1 ] into the new array newArr [ ] ; Print new array elements ; Driver Code
import java . util . * ; class GFG { static void constructNewArraySumZero ( int arr [ ] , int N ) { int newArr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( newArr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , - 5 , - 6 , 8 } ; int N = arr . length ; constructNewArraySumZero ( arr , N ) ; } }
Generate Bitonic Sequence of length N from integers in a given range | Java program for the above approach ; Function to construct bitonic sequence of length N from integers in the range [ L , R ] ; If sequence is not possible ; Store the resultant list ; If size of deque < n ; Add elements from start ; Print the stored in the list ; Driver Code ; Function Call
import java . util . * ; class GFG { public static void bitonicSequence ( int num , int lower , int upper ) { if ( num > ( upper - lower ) * 2 + 1 ) { System . out . println ( - 1 ) ; return ; } Deque < Integer > ans = new ArrayDeque < > ( ) ; for ( int i = 0 ; i < Math . min ( upper - lower + 1 , num - 1 ) ; i ++ ) ans . add ( upper - i ) ; for ( int i = 0 ; i < num - ans . size ( ) ; i ++ ) ans . addFirst ( upper - i - 1 ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 , L = 3 , R = 10 ; bitonicSequence ( N , L , R ) ; } }
Count substrings of same length differing by a single character from two given strings | Java program for the above approach ; Function to count the number of subStrings of equal length which differ by a single character ; Stores the count of pairs of subStrings ; Traverse the String s ; Traverse the String t ; Different character ; Increment the answer ; Count equal subStrings from next index ; Increment the count ; Increment q ; Check the condition ; Increment k ; Add q to count ; Decrement z ; Return the final count ; Driver Code ; Function Call
class GFG { static int countSubStrings ( String s , String t ) { int answ = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { for ( int j = 0 ; j < t . length ( ) ; j ++ ) { if ( t . charAt ( j ) != s . charAt ( i ) ) { answ += 1 ; int k = 1 ; int z = - 1 ; int q = 1 ; while ( j + z >= 0 && 0 <= i + z && s . charAt ( i + z ) == t . charAt ( j + z ) ) { z -= 1 ; answ += 1 ; q += 1 ; } while ( s . length ( ) > i + k && j + k < t . length ( ) && s . charAt ( i + k ) == t . charAt ( j + k ) ) { k += 1 ; answ += q ; z = - 1 ; } } } } return answ ; } public static void main ( String [ ] args ) { String S = " aba " ; String T = " baba " ; System . out . println ( countSubStrings ( S , T ) ) ; } }