codeparrot/xlcost-text-to-code huggingface dataset:
Count of Numbers in Range where the number does not contain more than K non zero digits | Java Program to find the count of numbers in a range where the number does not contain more than K non zero digits ; states - position , count , tight ; K is the number of non zero digits ; This function returns the count of required numbers from 0 to num ; Last position ; If count of non zero digits is less than or equal to K ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is nonzero increment currCnt ; At this position , number becomes smaller ; Next recursive call ; This function converts a number into its digit vector and uses above function to compute the answer ; Initialize dp ; Driver Code to test above functions
import java . util . * ; class Solution { static final int M = 20 ; static int dp [ ] [ ] [ ] = new int [ M ] [ M ] [ 2 ] ; static int K ; static Vector < Integer > num ; static int countInRangeUtil ( int pos , int cnt , int tight ) { if ( pos == num . size ( ) ) { if ( cnt <= K ) return 1 ; return 0 ; } if ( dp [ pos ] [ cnt ] [ tight ] != - 1 ) return dp [ pos ] [ cnt ] [ tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num . get ( pos ) ) ; for ( int dig = 0 ; dig <= limit ; dig ++ ) { int currCnt = cnt ; if ( dig != 0 ) currCnt ++ ; int currTight = tight ; if ( dig < num . get ( pos ) ) currTight = 1 ; ans += countInRangeUtil ( pos + 1 , currCnt , currTight ) ; } return dp [ pos ] [ cnt ] [ tight ] = ans ; } static int countInRange ( int x ) { num = new Vector < Integer > ( ) ; while ( x != 0 ) { num . add ( x % 10 ) ; x /= 10 ; } Collections . reverse ( num ) ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; return countInRangeUtil ( 0 , 0 , 0 ) ; } public static void main ( String args [ ] ) { int L = 1 , R = 1000 ; K = 3 ; System . out . println ( countInRange ( R ) - countInRange ( L - 1 ) ) ; L = 9995 ; R = 10005 ; K = 2 ; System . out . println ( countInRange ( R ) - countInRange ( L - 1 ) ) ; } }
Balanced expressions such that given positions have opening brackets | Set 2 | Java implementation of above approach using memoization ; Function to find Number of proper bracket expressions ; If open - closed brackets < 0 ; If index reaches the end of expression ; If brackets are balanced ; If already stored in dp ; If the current index has assigned open bracket ; Move forward increasing the length of open brackets ; Move forward by inserting open as well as closed brackets on that index ; return the answer ; Driver code ; DP array to precompute the answer ; Open brackets at position 1 ; Calling the find function to calculate the answer
public class GFG { static final int N = 1000 ; static int find ( int index , int openbrk , int n , int dp [ ] [ ] , int adj [ ] ) { if ( openbrk < 0 ) { return 0 ; } if ( index == n ) { if ( openbrk == 0 ) { return 1 ; } else { return 0 ; } } if ( dp [ index ] [ openbrk ] != - 1 ) { return dp [ index ] [ openbrk ] ; } if ( adj [ index ] == 1 ) { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) ; } else { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) + find ( index + 1 , openbrk - 1 , n , dp , adj ) ; } return dp [ index ] [ openbrk ] ; } public static void main ( String [ ] args ) { int dp [ ] [ ] = new int [ N ] [ N ] ; int n = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int adj [ ] = { 1 , 0 , 0 , 0 } ; System . out . print ( find ( 0 , 0 , 2 * n , dp , adj ) ) ; } }
Maximize array elements upto given number | Java code to find maximum value of number obtained by using array elements recursively . ; variable to store maximum value that can be obtained . ; Utility function to find maximum possible value ; If entire array is traversed , then compare current value in num to overall maximum obtained so far . ; Case 1 : Subtract current element from value so far if result is greater than or equal to zero . ; Case 2 : Add current element to value so far if result is less than or equal to maxLimit . ; Function to find maximum possible value that can be obtained using array elements and given number . ; variable to store current index position . ; call to utility function to find maximum possible value that can be obtained . ; Driver code
import java . io . * ; import java . lang . * ; public class GFG { static int ans ; static void findMaxValUtil ( int [ ] arr , int n , int num , int maxLimit , int ind ) { if ( ind == n ) { ans = Math . max ( ans , num ) ; return ; } if ( num - arr [ ind ] >= 0 ) { findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 ) ; } if ( num + arr [ ind ] <= maxLimit ) { findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 ) ; } } static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) { int ind = 0 ; findMaxValUtil ( arr , n , num , maxLimit , ind ) ; return ans ; } public static void main ( String args [ ] ) { int num = 1 ; int [ ] arr = { 3 , 10 , 6 , 4 , 5 } ; int n = arr . length ; int maxLimit = 15 ; System . out . print ( findMaxVal ( arr , n , num , maxLimit ) ) ; } }
Print equal sum sets of array ( Partition problem ) | Set 1 | Java program to print equal sum two subsets of an array if it can be partitioned into subsets . ; Declare Lists to store both the sets . ; / Function to print the equal sum sets of the array . ; / Print set 1. ; / Print set 2. ; Utility function to find the sets of the array which have equal sum . ; If entire array is traversed , compare both the sums . ; If sums are equal print both sets and return true to show sets are found . ; If sums are not equal then return sets are not found . ; Add current element to set 1. ; Recursive call after adding current element to set 1. ; If this inclusion results in equal sum sets partition then return true to show desired sets are found . ; If not then backtrack by removing current element from set1 and include it in set 2. ; Recursive call after including current element to set 2. ; Return true if array arr can be partitioned into two equal sum sets or not . ; Calculate sum of elements in array . ; If sum is odd then array cannot be partitioned . ; / Find both the sets . ; Driver code
import java . io . * ; import java . util . * ; public class GFG { static List < Integer > set1 = new ArrayList < Integer > ( ) ; static List < Integer > set2 = new ArrayList < Integer > ( ) ; static void printSets ( ) { int i ; for ( i = 0 ; i < set1 . size ( ) ; i ++ ) { System . out . print ( set1 . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; for ( i = 0 ; i < set2 . size ( ) ; i ++ ) { System . out . print ( set2 . get ( i ) + " ▁ " ) ; } } static boolean findSets ( Integer [ ] arr , int n , int sum1 , int sum2 , int pos ) { if ( pos == n ) { if ( sum1 == sum2 ) { printSets ( ) ; return true ; } else return false ; } set1 . add ( arr [ pos ] ) ; boolean res = findSets ( arr , n , sum1 + arr [ pos ] , sum2 , pos + 1 ) ; if ( res == true ) return res ; set1 . remove ( set1 . size ( ) - 1 ) ; set2 . add ( arr [ pos ] ) ; res = findSets ( arr , n , sum1 , sum2 + arr [ pos ] , pos + 1 ) ; if ( res == false ) if ( set2 . size ( ) > 0 ) set2 . remove ( set2 . size ( ) - 1 ) ; return res ; } static boolean isPartitionPoss ( Integer [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; return findSets ( arr , n , 0 , 0 , 0 ) ; } public static void main ( String args [ ] ) { Integer [ ] arr = { 5 , 5 , 1 , 11 } ; int n = arr . length ; if ( isPartitionPoss ( arr , n ) == false ) { System . out . print ( " - 1" ) ; } } }
Maximum subarray sum in O ( n ) using prefix sum | Java program to find out maximum subarray sum in linear time using prefix sum . ; Function to compute maximum subarray sum in linear time . ; Initialize minimum prefix sum to 0. ; Initialize maximum subarray sum so far to - infinity . ; Initialize and compute the prefix sum array . ; loop through the array , keep track of minimum prefix sum so far and maximum subarray sum . ; Driver Program ; Test case 1 ; Test case 2
class GFG { static int maximumSumSubarray ( int arr [ ] , int n ) { int min_prefix_sum = 0 ; int res = Integer . MIN_VALUE ; int prefix_sum [ ] = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { res = Math . max ( res , prefix_sum [ i ] - min_prefix_sum ) ; min_prefix_sum = Math . min ( min_prefix_sum , prefix_sum [ i ] ) ; } return res ; } public static void main ( String [ ] args ) { int arr1 [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n1 = arr1 . length ; System . out . println ( maximumSumSubarray ( arr1 , n1 ) ) ; int arr2 [ ] = { 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 } ; int n2 = arr2 . length ; System . out . println ( maximumSumSubarray ( arr2 , n2 ) ) ; } }
Counting numbers of n digits that are monotone | Java program to count numbers of n digits that are monotone . ; Considering all possible digits as { 1 , 2 , 3 , . .9 } ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits . ; Unit length numbers ; Single digit numbers ; Filling rest of the entries in bottom up manner . ; Driver code .
class GFG { static final int DP_s = 9 ; static int getNumMonotone ( int len ) { int [ ] [ ] DP = new int [ len ] [ DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 0 ; i < len ; ++ i ) DP [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] ; return DP [ len - 1 ] [ DP_s - 1 ] ; } public static void main ( String [ ] args ) { System . out . println ( getNumMonotone ( 10 ) ) ; } }
Newman | JAVA Code for Newman - Conway Sequence ; Function to find the n - th element ; Declare array to store sequence ; Driver program to test above function
import java . util . * ; class GFG { static int sequence ( int n ) { int f [ ] = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; int i ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( sequence ( n ) ) ; } }
Maximum product of an increasing subsequence | Dynamic programming Java implementation of maximum product of an increasing subsequence ; Returns product of maximum product increasing subsequence . ; Initialize MPIS values ; Compute optimized MPIS values considering every element as ending element of sequence ; Pick maximum of all product values using for loop ; Driver program to test above function
import java . util . Arrays ; import java . util . Collections ; class GFG { static int lis ( int [ ] arr , int n ) { int [ ] mpis = new int [ n ] ; int max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; for ( int k = 0 ; k < mpis . length ; k ++ ) { if ( mpis [ k ] > max ) { max = mpis [ k ] ; } } return max ; } static public void main ( String [ ] args ) { int [ ] arr = { 3 , 100 , 4 , 5 , 150 , 6 } ; int n = arr . length ; System . out . println ( lis ( arr , n ) ) ; } }
Hosoya 's Triangle | Java Program to print Hosoya 's triangle of height n. ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driver program to test above function
import java . util . * ; class GFG { static int Hosoya ( int n , int m ) { if ( ( n == 0 && m == 0 ) || ( n == 1 && m == 0 ) || ( n == 1 && m == 1 ) || ( n == 2 && m == 1 ) ) return 1 ; if ( n > m ) return Hosoya ( n - 1 , m ) + Hosoya ( n - 2 , m ) ; else if ( m == n ) return Hosoya ( n - 1 , m - 1 ) + Hosoya ( n - 2 , m - 2 ) ; else return 0 ; } static void printHosoya ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( Hosoya ( i , j ) + " ▁ " ) ; System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printHosoya ( n ) ; } }
Eulerian Number | Java program to find Eulerian number A ( n , m ) ; Return eulerian number A ( n , m ) ; driver code
import java . util . * ; class Eulerian { public static int eulerian ( int n , int m ) { if ( m >= n n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; } public static void main ( String [ ] args ) { int n = 3 , m = 1 ; System . out . print ( eulerian ( n , m ) ) ; } }
Largest divisible pairs subset | Java program to find the largest subset which was each pair is divisible . ; function to find the longest Subsequence ; dp [ i ] is going to store size of largest divisible subset beginning with a [ i ] . ; Since last element is largest , d [ n - 1 ] is 1 ; Fill values for smaller elements . ; Find all multiples of a [ i ] and consider the multiple that has largest subset beginning with it . ; Return maximum value from dp [ ] ; driver code to check the above function
import java . util . Arrays ; class GFG { static int largestSubset ( int [ ] a , int n ) { int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int mxm = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] % a [ i ] == 0 a [ i ] % a [ j ] == 0 ) { mxm = Math . max ( mxm , dp [ j ] ) ; } } dp [ i ] = 1 + mxm ; } return Arrays . stream ( dp ) . max ( ) . getAsInt ( ) ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 3 , 6 , 13 , 17 , 18 } ; int n = a . length ; System . out . println ( largestSubset ( a , n ) ) ; } }
Friends Pairing Problem | Java program for solution of friends pairing problem ; Returns count of ways n people can remain single or paired up . ; Filling dp [ ] in bottom - up manner using recursive formula explained above . ; Driver code
import java . io . * ; class GFG { static int countFriendsPairings ( int n ) { int dp [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i <= 2 ) dp [ i ] = i ; else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countFriendsPairings ( n ) ) ; } }
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code
class GFG { static int countFriendsPairings ( int n ) { int a = 1 , b = 2 , c = 0 ; if ( n <= 2 ) { return n ; } for ( int i = 3 ; i <= n ; i ++ ) { c = b + ( i - 1 ) * a ; a = b ; b = c ; } return c ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countFriendsPairings ( n ) ) ; } }
Check whether row or column swaps produce maximum size binary sub | Java program to find maximum binary sub - matrix with row swaps and column swaps . ; Precompute the number of consecutive 1 below the ( i , j ) in j - th column and the number of consecutive 1 s on right side of ( i , j ) in i - th row . ; Travesing the 2d matrix from top - right . ; If ( i , j ) contain 0 , do nothing ; } Counting consecutive 1 on right side ; Travesing the 2d matrix from bottom - left . ; If ( i , j ) contain 0 , do nothing ; } Counting consecutive 1 down to ( i , j ) . ; Return maximum size submatrix with row swap allowed . ; Copying the column ; Sort the copied array ; Find maximum submatrix size . ; Return maximum size submatrix with column swap allowed . ; Copying the row . ; Sort the copied array ; Find maximum submatrix size . ; Solving for row swap and column swap ; Comparing both . ; Driven Program
import java . util . Arrays ; class GFG { static int R = 5 ; static int C = 3 ; static void precompute ( int mat [ ] [ ] , int ryt [ ] [ ] , int dwn [ ] [ ] ) { for ( int j = C - 1 ; j >= 0 ; j -- ) { for ( int i = 0 ; i < R ; ++ i ) { if ( mat [ i ] [ j ] == 0 ) { ryt [ i ] [ j ] = 0 ; else { ryt [ i ] [ j ] = ryt [ i ] [ j + 1 ] + 1 ; } } } for ( int i = R - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < C ; ++ j ) { if ( mat [ i ] [ j ] == 0 ) { dwn [ i ] [ j ] = 0 ; else { dwn [ i ] [ j ] = dwn [ i + 1 ] [ j ] + 1 ; } } } } static int solveRowSwap ( int ryt [ ] [ ] ) { int b [ ] = new int [ R ] , ans = 0 ; for ( int j = 0 ; j < C ; j ++ ) { for ( int i = 0 ; i < R ; i ++ ) { b [ i ] = ryt [ i ] [ j ] ; } Arrays . sort ( b ) ; for ( int i = 0 ; i < R ; ++ i ) { ans = Math . max ( ans , b [ i ] * ( R - i ) ) ; } } return ans ; } static int solveColumnSwap ( int dwn [ ] [ ] ) { int b [ ] = new int [ C ] , ans = 0 ; for ( int i = 0 ; i < R ; ++ i ) { for ( int j = 0 ; j < C ; ++ j ) { b [ j ] = dwn [ i ] [ j ] ; } Arrays . sort ( b ) ; for ( int k = 0 ; k < C ; ++ k ) { ans = Math . max ( ans , b [ k ] * ( C - k ) ) ; } } return ans ; } static void findMax1s ( int mat [ ] [ ] ) { int ryt [ ] [ ] = new int [ R + 2 ] [ C + 2 ] , dwn [ ] [ ] = new int [ R + 2 ] [ C + 2 ] ; precompute ( mat , ryt , dwn ) ; int rswap = solveRowSwap ( ryt ) ; int cswap = solveColumnSwap ( dwn ) ; if ( rswap > cswap ) { System . out . println ( "Row SwapNEW_LINE" + rswap); } else { System . out . println ( "Column SwapNEW_LINE" + cswap); } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 0 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } , { 0 , 0 , 0 } , { 1 , 1 , 0 } } ; findMax1s ( mat ) ; } }
LCS ( Longest Common Subsequence ) of three strings | Java program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] [ o + 1 ] in bottom up fashion . Note that L [ i ] [ j ] [ k ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] and Z [ 0. . ... k - 1 ] ; L [ m ] [ n ] [ o ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] and Z [ 0. . o - 1 ] ; Driver program to test above function
public class LCS_3Strings { static int lcsOf3 ( String X , String Y , String Z , int m , int n , int o ) { int [ ] [ ] [ ] L = new int [ m + 1 ] [ n + 1 ] [ o + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { for ( int k = 0 ; k <= o ; k ++ ) { if ( i == 0 j == 0 k == 0 ) L [ i ] [ j ] [ k ] = 0 ; else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) && X . charAt ( i - 1 ) == Z . charAt ( k - 1 ) ) L [ i ] [ j ] [ k ] = L [ i - 1 ] [ j - 1 ] [ k - 1 ] + 1 ; else L [ i ] [ j ] [ k ] = Math . max ( Math . max ( L [ i - 1 ] [ j ] [ k ] , L [ i ] [ j - 1 ] [ k ] ) , L [ i ] [ j ] [ k - 1 ] ) ; } } } return L [ m ] [ n ] [ o ] ; } public static void main ( String args [ ] ) { String X = " AGGT12" ; String Y = "12TXAYB " ; String Z = "12XBA " ; int m = X . length ( ) ; int n = Y . length ( ) ; int o = Z . length ( ) ; System . out . println ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcsOf3 ( X , Y , Z , m , n , o ) ) ; } }
Longest subsequence such that difference between adjacents is one | Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }
import java . lang . Math ; import java . util . * ; class GFG { static int longestSubsequence ( int n , int arr [ ] ) { if ( n == 1 ) return 1 ; Integer dp [ ] = new Integer [ n ] ; HashMap < Integer , Integer > mapp = new HashMap < > ( ) ; dp [ 0 ] = 1 ; mapp . put ( arr [ 0 ] , 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i ] - arr [ i - 1 ] ) == 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; else { if ( mapp . containsKey ( arr [ i ] + 1 ) || mapp . containsKey ( arr [ i ] - 1 ) ) { dp [ i ] = 1 + Math . max ( mapp . getOrDefault ( arr [ i ] + 1 , 0 ) , mapp . getOrDefault ( arr [ i ] - 1 , 0 ) ) ; } else dp [ i ] = 1 ; } mapp . put ( arr [ i ] , dp [ i ] ) ; } return Collections . max ( Arrays . asList ( dp ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( longestSubsequence ( n , arr ) ) ; } }
Printing Shortest Common Supersequence | A dynamic programming based Java program print shortest supersequence of two strings ; returns shortest supersequence of X and Y ; dp [ i ] [ j ] contains length of shortest supersequence for X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Fill table in bottom up manner ; Below steps follow recurrence relation ; string to store the shortest supersequence ; Start from the bottom right corner and one by one push characters in output string ; If current character in X and Y are same , then current character is part of shortest supersequence ; Put current character in result ; reduce values of i , j and index ; If current character in X and Y are different ; Put current character of Y in result ; reduce values of j and index ; Put current character of X in result ; reduce values of i and index ; If Y reaches its end , put remaining characters of X in the result string ; If X reaches its end , put remaining characters of Y in the result string ; reverse the string and return it ; Swap values of left and right ; Driver code
class GFG { static String printShortestSuperSeq ( String X , String Y ) { int m = X . length ( ) ; int n = Y . length ( ) ; int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) { dp [ i ] [ j ] = j ; } else if ( j == 0 ) { dp [ i ] [ j ] = i ; } else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } } String str = " " ; int i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { str += ( X . charAt ( i - 1 ) ) ; i -- ; j -- ; } else if ( dp [ i - 1 ] [ j ] > dp [ i ] [ j - 1 ] ) { str += ( Y . charAt ( j - 1 ) ) ; j -- ; } else { str += ( X . charAt ( i - 1 ) ) ; i -- ; } } while ( i > 0 ) { str += ( X . charAt ( i - 1 ) ) ; i -- ; } while ( j > 0 ) { str += ( Y . charAt ( j - 1 ) ) ; j -- ; } str = reverse ( str ) ; return str ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; System . out . println ( printShortestSuperSeq ( X , Y ) ) ; } }
Longest Repeating Subsequence | Java program to find the longest repeating subsequence ; Function to find the longest repeating subsequence ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match ; driver program to check above function
import java . io . * ; import java . util . * ; class LRS { static int findLongestRepeatingSubSeq ( String str ) { int n = str . length ( ) ; int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str . charAt ( i - 1 ) == str . charAt ( j - 1 ) && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ n ] ; } public static void main ( String [ ] args ) { String str = " aabb " ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ largest ▁ subsequence ▁ that " + " ▁ repeats ▁ itself ▁ is ▁ : ▁ " + findLongestRepeatingSubSeq ( str ) ) ; } }