codeparrot/xlcost-text-to-code huggingface dataset:
Number of distinct words of size N with at most K contiguous vowels | Java program for the above approach ; Power function to calculate long powers with mod ; Function for finding number of ways to create string with length N and atmost K contiguous vowels ; Array dp to store number of ways ; dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] . . dp [ i - 1 ] [ k ] ) * 21 ; Now setting sum to be dp [ i ] [ 0 ] ; If j > i , no ways are possible to create a string with length i and vowel j ; If j = i all the character should be vowel ; dp [ i ] [ j ] relation with dp [ i - 1 ] [ j - 1 ] ; Adding dp [ i ] [ j ] in the sum ; Driver Code ; Input ; Function Call
class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int kvowelwords ( int N , int K ) { int i , j ; int MOD = 1000000007 ; int [ ] [ ] dp = new int [ N + 1 ] [ K + 1 ] ; int sum = 1 ; for ( i = 1 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = sum * 21 ; dp [ i ] [ 0 ] %= MOD ; sum = dp [ i ] [ 0 ] ; for ( j = 1 ; j <= K ; j ++ ) { if ( j > i ) dp [ i ] [ j ] = 0 ; else if ( j == i ) { dp [ i ] [ j ] = power ( 5 , i , MOD ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] * 5 ; } dp [ i ] [ j ] %= MOD ; sum += dp [ i ] [ j ] ; sum %= MOD ; } } return sum ; } public static void main ( String [ ] args ) { int N = 3 ; int K = 3 ; System . out . println ( kvowelwords ( N , K ) ) ; } }
Maximize the length of upper boundary formed by placing given N rectangles horizontally or vertically | Java program for the above approach ; Utility class ; Function to find maximum length of the upper boundary formed by placing each of the rectangles either horizontally or vertically ; Stores the intermediate transition states ; Place the first rectangle horizontally ; Place the first rectangle vertically ; Place horizontally ; Stores the difference in height of current and previous rectangle ; Take maximum out of two options ; Place Vertically ; Stores the difference in height of current and previous rectangle ; Take maximum out two options ; Print maximum of horizontal or vertical alignment of the last rectangle ; Driver code
import java . util . Vector ; public class GFG { public static class pair { private int first ; private int second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void maxBoundary ( int N , Vector < pair > V ) { int dp [ ] [ ] = new int [ N ] [ 2 ] ; dp [ 0 ] [ 0 ] = V . get ( 0 ) . first ; dp [ 0 ] [ 1 ] = V . get ( 0 ) . second ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = V . get ( i ) . first ; int height1 = Math . abs ( V . get ( i - 1 ) . second - V . get ( i ) . second ) ; int height2 = Math . abs ( V . get ( i - 1 ) . first - V . get ( i ) . second ) ; dp [ i ] [ 0 ] += Math . max ( height1 + dp [ i - 1 ] [ 0 ] , height2 + dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = V . get ( i ) . second ; int vertical1 = Math . abs ( V . get ( i ) . first - V . get ( i - 1 ) . second ) ; int vertical2 = Math . abs ( V . get ( i ) . first - V . get ( i - 1 ) . first ) ; dp [ i ] [ 1 ] += Math . max ( vertical1 + dp [ i - 1 ] [ 0 ] , vertical2 + dp [ i - 1 ] [ 1 ] ) ; } System . out . println ( Math . max ( dp [ N - 1 ] [ 0 ] , dp [ N - 1 ] [ 1 ] ) ) ; } public static void main ( String [ ] args ) { int N = 5 ; Vector < pair > V = new Vector < > ( ) ; V . add ( new pair ( 2 , 5 ) ) ; V . add ( new pair ( 3 , 8 ) ) ; V . add ( new pair ( 1 , 10 ) ) ; V . add ( new pair ( 7 , 14 ) ) ; V . add ( new pair ( 2 , 5 ) ) ; maxBoundary ( N , V ) ; } }
Find maximum subset | Java program for the above approach ; Dp vector ; current element ; current element modulo D ; copy previous state ; Transitions ; return answer ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static int maximumSum ( int [ ] A , int N , int K , int D ) { int [ ] [ ] [ ] dp = new int [ N + 1 ] [ K + 1 ] [ D + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < K + 1 ; j ++ ) { for ( int k = 0 ; k < D + 1 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } for ( int i = 1 ; i <= N ; i ++ ) { int element = A [ i - 1 ] ; int mod = A [ i - 1 ] % D ; dp [ i ] = dp [ i - 1 ] ; for ( int j = 1 ; j <= K ; j ++ ) { dp [ i ] [ j ] [ mod ] = Math . max ( dp [ i ] [ j ] [ mod ] , element ) ; for ( int p = 0 ; p < D ; p ++ ) { if ( dp [ i - 1 ] [ j - 1 ] [ p ] != - 1 ) { dp [ i ] [ j ] [ ( p + mod ) % D ] = Math . max ( dp [ i ] [ j ] [ ( p + mod ) % D ] , dp [ i - 1 ] [ j - 1 ] [ p ] + element ) ; } } } } if ( dp [ N ] [ K ] [ 0 ] == - 1 ) return 0 ; return dp [ N ] [ K ] [ 0 ] ; } public static void main ( String [ ] args ) { int N = 5 , K = 3 , D = 7 ; int [ ] A = { 1 , 11 , 5 , 5 , 18 } ; System . out . print ( maximumSum ( A , N , K , D ) ) ; } }
Ways to sum to N using Natural Numbers up to K with repetitions allowed | Java program for the above approach ; Function to find the total number of ways to represent N as the sum of integers over the range [ 1 , K ] ; Initialize a list ; Update dp [ 0 ] to 1 ; Iterate over the range [ 1 , K + 1 ] ; Iterate over the range [ 1 , N + 1 ] ; If col is greater than or equal to row ; Update current dp [ col ] state ; Return the total number of ways ; Driver code ; Given inputs
import java . util . * ; class GFG { static int NumberOfways ( int N , int K ) { int [ ] dp = new int [ N + 1 ] ; dp [ 0 ] = 1 ; for ( int row = 1 ; row < K + 1 ; row ++ ) { for ( int col = 1 ; col < N + 1 ; col ++ ) { if ( col >= row ) dp [ col ] = dp [ col ] + dp [ col - row ] ; } } return ( dp [ N ] ) ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 2 ; System . out . println ( NumberOfways ( N , K ) ) ; } }
Queries to check if array elements from indices [ L , R ] forms an Arithmetic Progression or not | Java program for the above approach ; Function to check if the given range of queries form an AP or not in the given array arr [ ] ; Stores length of the longest subarray forming AP for every array element ; Iterate over the range [ 0 , N ] ; Stores the index of the last element of forming AP ; Iterate until the element at index ( j , j + 1 ) forms AP ; Increment j by 1 ; Traverse the current subarray over the range [ i , j - 1 ] ; Update the length of the longest subarray at index k ; Update the value of i ; Traverse the given queries ; Print the result ; Otherwise ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findAPSequence ( int arr [ ] , int N , int Q [ ] [ ] , int M ) { int dp [ ] = new int [ N + 5 ] ; for ( int i = 0 ; i + 1 < N ; ) { int j = i + 1 ; while ( j + 1 < N && arr [ j + 1 ] - arr [ j ] == arr [ i + 1 ] - arr [ i ] ) j ++ ; for ( int k = i ; k < j ; k ++ ) { dp [ k ] = j - k ; } i = j ; } for ( int i = 0 ; i < M ; i ++ ) { if ( dp [ Q [ i ] [ 0 ] ] >= Q [ i ] [ 1 ] - Q [ i ] [ 0 ] ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 6 , 5 , 4 , 1 } ; int Q [ ] [ ] = { { 0 , 3 } , { 3 , 4 } , { 2 , 4 } } ; int N = arr . length ; int M = Q . length ; findAPSequence ( arr , N , Q , M ) ; } }
Minimize difference between sum of two K | Java program for the above approach ; Stores the values at recursive states ; Function to find the minimum difference between sum of two K - length subsets ; Base Case ; If k1 and k2 are 0 , then return the absolute difference between sum1 and sum2 ; Otherwise , return INT_MAX ; If the result is already computed , return the result ; Store the 3 options ; Including the element in first subset ; Including the element in second subset ; Not including the current element in both the subsets ; Store minimum of 3 values obtained ; Return the value for the current states ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int minSumDifference ( int [ ] arr , int n , int k1 , int k2 , int sum1 , int sum2 ) { if ( n < 0 ) { if ( k1 == 0 && k2 == 0 ) { return Math . abs ( sum1 - sum2 ) ; } else { return Integer . MAX_VALUE ; } } if ( dp [ n ] [ sum1 ] [ sum2 ] != - 1 ) { return dp [ n ] [ sum1 ] [ sum2 ] ; } int op1 = Integer . MAX_VALUE ; int op2 = Integer . MAX_VALUE ; int op3 = Integer . MAX_VALUE ; if ( k1 > 0 ) { op1 = minSumDifference ( arr , n - 1 , k1 - 1 , k2 , sum1 + arr [ n ] , sum2 ) ; } if ( k2 > 0 ) { op2 = minSumDifference ( arr , n - 1 , k1 , k2 - 1 , sum1 , sum2 + arr [ n ] ) ; } op3 = minSumDifference ( arr , n - 1 , k1 , k2 , sum1 , sum2 ) ; dp [ n ] [ sum1 ] [ sum2 ] = Math . min ( op1 , Math . min ( op2 , op3 ) ) ; return dp [ n ] [ sum1 ] [ sum2 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 3 , 5 , 6 , 7 , 17 } ; int K = 2 ; int N = arr . length ; for ( int [ ] [ ] row : dp ) { for ( int [ ] innerrow : row ) { Arrays . fill ( innerrow , - 1 ) ; } } System . out . println ( minSumDifference ( arr , N - 1 , K , K , 0 , 0 ) ) ; } }
Geek | Java program for the above approach ; Function to calculate the N - th Geek - onacci Number ; Stores the geekonacci series ; Store the first three terms of the series ; Iterate over the range [ 3 , N ] ; Update the value of arr [ i ] as the sum of previous 3 terms in the series ; Return the last element of arr [ ] as the N - th term ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int find ( int A , int B , int C , int N ) { int [ ] arr = new int [ N ] ; arr [ 0 ] = A ; arr [ 1 ] = B ; arr [ 2 ] = C ; for ( int i = 3 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] + arr [ i - 3 ] ; } return arr [ N - 1 ] ; } public static void main ( String [ ] args ) { int A = 1 , B = 3 , C = 2 , N = 4 ; System . out . print ( find ( A , B , C , N ) ) ; } }
Maximum sum subsequence made up of at most K distant elements including the first and last array elements | Java program for the above approach ; Pair class Store ( x , y ) Pair ; Function to find maximum sum of a subsequence satisfying the given conditions ; Stores the maximum sum ; Starting index of the subsequence ; Stores the pair of maximum value and the index of that value ; Traverse the array ; Increment the first value of deque by arr [ i ] and store it in dp [ i ] ; Delete all the values which are less than dp [ i ] in deque ; Append the current pair of value and index in deque ; If first value of the queue is at a distance > K ; Return the value at the last index ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static class Pair { int x , y ; Pair ( int x , int y ) { this . x = x ; this . y = y ; } } private static int maxResult ( int [ ] arr , int k , int n ) { int dp [ ] = new int [ n ] ; dp [ 0 ] = arr [ 0 ] ; Deque < Pair > q = new LinkedList < Pair > ( ) ; q . add ( new Pair ( arr [ 0 ] , 0 ) ) ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] + q . peekFirst ( ) . x ; while ( q . size ( ) > 0 && q . peekLast ( ) . x < dp [ i ] ) q . pollLast ( ) ; q . add ( new Pair ( dp [ i ] , i ) ) ; if ( i - k == q . peekFirst ( ) . y ) q . pollFirst ( ) ; } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , - 5 , - 2 , 4 , 0 , 3 } ; int K = 3 ; int n = arr . length ; System . out . println ( maxResult ( arr , K , n ) ) ; } }
Minimum days required to cure N persons | Java Program for the above approach ; Function to find minimum count of days required to give a cure such that the high risk person and risk person does not get a dose on same day . ; Stores count of persons whose age is less than or equal to 10 and greater than or equal to 60. ; Stores the count of persons whose age is in the range [ 11 , 59 ] ; Traverse the array arr [ ] ; If age less than or equal to 10 or greater than or equal to 60 ; Update risk ; Update normal_risk ; Calculate days to cure risk and normal_risk persons ; Print the days ; Given array ; Size of the array ; Given P
class GFG { static void daysToCure ( int arr [ ] , int N , int P ) { int risk = 0 ; int normal_risk = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 60 arr [ i ] <= 10 ) { risk ++ ; } else { normal_risk ++ ; } } int days = ( risk / P ) + ( normal_risk / P ) ; if ( risk % P > 0 ) { days ++ ; } if ( normal_risk % P > 0 ) { days ++ ; } System . out . print ( days ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 80 , 27 , 72 , 79 } ; int N = arr . length ; int P = 2 ; daysToCure ( arr , N , P ) ; } }
Count numbers from a given range whose product of digits is K | Java program to implement the above approach ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; If count of digits in a number greater than count of digits in X ; If product of digits of a number equal to K ; If overlapping subproblems already occurred ; Stores count of numbers whose product of digits is K ; Check if the numbers exceeds K or not ; Iterate over all possible value of i - th digits ; If number contains leading 0 ; Update res ; Update res ; Utility function to count the numbers in the range [ L , R ] whose prod of digits is K ; Stores numbers in the form of String ; Stores overlapping subproblems ; Initialize dp [ ] [ ] [ ] to - 1 ; Stores count of numbers in the range [ 0 , R ] whose product of digits is k ; Update str ; Initialize dp [ ] [ ] [ ] to - 1 ; Stores count of numbers in the range [ 0 , L - 1 ] whose product of digits is k ; Driver Code
import java . util . * ; class GFG { static final int M = 100 ; static int cntNum ( String X , int i , int prod , int K , int st , int tight , int [ ] [ ] [ ] [ ] dp ) { if ( i >= X . length ( ) prod > K ) { return prod == K ? 1 : 0 ; } if ( dp [ prod ] [ i ] [ tight ] [ st ] != - 1 ) { return dp [ prod ] [ i ] [ tight ] [ st ] ; } int res = 0 ; int end = tight > 0 ? X . charAt ( i ) - '0' : 9 ; for ( int j = 0 ; j <= end ; j ++ ) { if ( j == 0 && st == 0 ) { res += cntNum ( X , i + 1 , prod , K , 0 , ( tight & ( ( j == end ) ? 1 : 0 ) ) , dp ) ; } else { res += cntNum ( X , i + 1 , prod * j , K , 1 , ( tight & ( ( j == end ) ? 1 : 0 ) ) , dp ) ; } } static int UtilCntNumRange ( int L , int R , int K ) { String str = String . valueOf ( R ) ; int [ ] [ ] [ ] [ ] dp = new int [ M ] [ M ] [ 2 ] [ 2 ] ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } int cntR = cntNum ( str , 0 , 1 , K , 0 , 1 , dp ) ; str = String . valueOf ( L - 1 ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } int cntL = cntNum ( str , 0 , 1 , K , 0 , 1 , dp ) ; return ( cntR - cntL ) ; } public static void main ( String [ ] args ) { int L = 20 , R = 10000 , K = 14 ; System . out . print ( UtilCntNumRange ( L , R , K ) ) ; } }
Count all possible N | Java program for the above approach ; Function to find the number of vowel permutations possible ; To avoid the large output value ; Initialize 2D dp array ; Initialize dp [ 1 ] [ i ] as 1 since string of length 1 will consist of only one vowel in the string ; Directed graph using the adjacency matrix ; Iterate over the range [ 1 , N ] ; Traverse the directed graph ; Traversing the list ; Update dp [ i + 1 ] [ u ] ; Stores total count of permutations ; Return count of permutations ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int countVowelPermutation ( int n ) { int MOD = ( int ) ( 1e9 + 7 ) ; long [ ] [ ] dp = new long [ n + 1 ] [ 5 ] ; for ( int i = 0 ; i < 5 ; i ++ ) { dp [ 1 ] [ i ] = 1 ; } int [ ] [ ] relation = new int [ ] [ ] { { 1 } , { 0 , 2 } , { 0 , 1 , 3 , 4 } , { 2 , 4 } , { 0 } } ; for ( int i = 1 ; i < n ; i ++ ) { for ( int u = 0 ; u < 5 ; u ++ ) { dp [ i + 1 ] [ u ] = 0 ; for ( int v : relation [ u ] ) { dp [ i + 1 ] [ u ] += dp [ i ] [ v ] % MOD ; } } } long ans = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans = ( ans + dp [ n ] [ i ] ) % MOD ; } return ( int ) ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( countVowelPermutation ( N ) ) ; } }
Queries to calculate Bitwise OR of each subtree of a given node in an N | Java program for above approach ; Maximum Number of nodes ; Adjacency list ; Stores Bitwise OR of each node ; Function to add edges to the Tree ; Traverse the edges ; Add edges ; Function to perform DFS Traversal on the given tree ; Initialize answer with bitwise OR of current node ; Iterate over each child of the current node ; Skip parent node ; Call DFS for each child ; Taking bitwise OR of the answer of the child to find node 's OR value ; Function to call DFS from the '=  root for precomputing answers ; Function to calculate and print the Bitwise OR for Q queries ; Perform preprocessing ; Iterate over each given query ; Utility function to find and print bitwise OR for Q queries ; Function to add edges to graph ; Function call ; Driver function ; Number of nodes ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int N = ( int ) 1e5 + 5 ; static ArrayList < ArrayList < Integer > > adj ; static int [ ] answer ; static void addEdgesToGraph ( int Edges [ ] [ ] , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } } static void DFS ( int node , int parent , int Val [ ] ) { answer [ node ] = Val [ node ] ; for ( Integer child : adj . get ( node ) ) { if ( child == parent ) continue ; DFS ( child , node , Val ) ; answer [ node ] = ( answer [ node ] answer [ child ] ) ; } } static void preprocess ( int Val [ ] ) { DFS ( 1 , - 1 , Val ) ; } static void findSubtreeOR ( int Queries [ ] , int Q , int Val [ ] ) { preprocess ( Val ) ; for ( int i = 0 ; i < Q ; i ++ ) { System . out . println ( answer [ Queries [ i ] ] + " ▁ " ) ; } } static void findSubtreeORUtil ( int N , int Edges [ ] [ ] , int Val [ ] , int Queries [ ] , int Q ) { addEdgesToGraph ( Edges , N ) ; findSubtreeOR ( Queries , Q , Val ) ; } public static void main ( String [ ] args ) { adj = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) adj . add ( new ArrayList < > ( ) ) ; answer = new int [ N ] ; N = 5 ; int Edges [ ] [ ] = { { 1 , 2 } , { 1 , 3 } , { 3 , 4 } , { 3 , 5 } } ; int Val [ ] = { 0 , 2 , 3 , 4 , 8 , 16 } ; int Queries [ ] = { 2 , 3 , 1 } ; int Q = Queries . length ; findSubtreeORUtil ( N , Edges , Val , Queries , Q ) ; } }
Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements | Java program for the above approach ; Function to return the maximum of two elements ; Function to find the maximum sum of selecting K elements from the given 2D array arr [ ] [ ] ; dp table of size ( K + 1 ) * ( N + 1 ) ; Initialize dp [ 0 ] [ i ] = 0 ; Initialize dp [ i ] [ 0 ] = 0 ; Selecting i elements ; Select i elements till jth row ; sum = 0 , to keep track of cummulative elements sum ; Traverse arr [ j ] [ k ] until number of elements until k > i ; Select arr [ j ] [ k - 1 ] th item ; Store the maxSum in dp [ i ] [ j + 1 ] ; Return the maximum sum ; Driver Code ; Function Call
import java . util . * ; class GFG { static int max ( int a , int b ) { return a > b ? a : b ; } static int maximumsum ( int arr [ ] [ ] , int K , int N , int M ) { int sum = 0 , maxSum ; int i , j , k ; int [ ] [ ] dp = new int [ K + 1 ] [ N + 1 ] ; for ( i = 0 ; i <= N ; i ++ ) dp [ 0 ] [ i ] = 0 ; for ( i = 0 ; i <= K ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( i = 1 ; i <= K ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { sum = 0 ; maxSum = dp [ i ] [ j ] ; for ( k = 1 ; k <= M && k <= i ; k ++ ) { sum += arr [ j ] [ k - 1 ] ; maxSum = Math . max ( maxSum , sum + dp [ i - k ] [ j ] ) ; } dp [ i ] [ j + 1 ] = maxSum ; } } return dp [ K ] [ N ] ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 10 , 10 , 100 , 30 } , { 80 , 50 , 10 , 50 } } ; int N = 2 , M = 4 ; int K = 5 ; System . out . print ( maximumsum ( arr , K , N , M ) ) ; } }
Subsequences of given string consisting of non | Javs program to implement the above approach ; Function to find all the subsequences of the string with non - repeating characters ; Base case ; Insert current subsequence ; If str [ i ] is not present in the current subsequence ; Insert str [ i ] into the set ; Insert str [ i ] into the current subsequence ; Remove str [ i ] from the set ; Not including str [ i ] from the current subsequence ; Utility function to print all subsequences of string with non - repeating characters ; Stores all possible subsequences with non - repeating characters ; Stores subsequence with non - repeating characters ; Traverse all possible subsequences containing non - repeating characters ; Print subsequence ; Driver Code
import java . util . * ; class GFG { public static void FindSub ( HashSet < String > sub , HashSet < Character > ch , String str , String res , int i ) { if ( i == str . length ( ) ) { sub . add ( res ) ; return ; } if ( ! ch . contains ( str . charAt ( i ) ) ) { ch . add ( str . charAt ( i ) ) ; FindSub ( sub , ch , str , res + str . charAt ( i ) , i + 1 ) ; ch . remove ( str . charAt ( i ) ) ; } FindSub ( sub , ch , str , res , i + 1 ) ; } public static void printSubwithUniqueChar ( String str , int N ) { HashSet < String > sub = new HashSet < > ( ) ; HashSet < Character > ch = new HashSet < > ( ) ; FindSub ( sub , ch , str , " " , 0 ) ; for ( String subString : sub ) { System . out . print ( subString + " ▁ " ) ; } } public static void main ( String args [ ] ) { String str = " abac " ; int N = str . length ( ) ; printSubwithUniqueChar ( str , N ) ; } }
Count lexicographically increasing K | Java program for the above approach ; Function to count K - length strings from first N alphabets ; To keep track of column sum in dp ; Auxiliary 2d dp array ; Initialize dp [ 0 ] [ i ] = 1 and update the column_sum ; Iterate for K times ; Iterate for N times ; dp [ i ] [ j ] : Stores the number of ways to form i - length strings consisting of j letters ; Update the column_sum ; Print number of ways to arrange K - length strings with N alphabets ; Driver Code ; Given N and K ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void waysToArrangeKLengthStrings ( int N , int K ) { int [ ] column_sum = new int [ N + 1 ] ; int i , j ; for ( i = 1 ; i < N + 1 ; i ++ ) { column_sum [ i ] = 0 ; } int dp [ ] [ ] = new int [ K + 1 ] [ N + 1 ] ; for ( i = 1 ; i < K + 1 ; i ++ ) { for ( j = 1 ; j < N + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= N ; i ++ ) { dp [ 0 ] [ i ] = 1 ; column_sum [ i ] = 1 ; } for ( i = 1 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { dp [ i ] [ j ] += column_sum [ j - 1 ] ; column_sum [ j ] += dp [ i ] [ j ] ; } } System . out . print ( dp [ K ] [ N ] ) ; } public static void main ( String [ ] args ) { int N = 5 , K = 2 ; waysToArrangeKLengthStrings ( N , K ) ; } }
Count N | Java program to implement the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Stores count of strings consisting of vowels sorted lexicographically of all possible lengths ; Initialize DP [ 1 ] [ 1 ] ; Traverse the matrix row - wise ; Base Case ; Return the result ; Driver Code ; Function Call
import java . util . * ; class GFG { static int findNumberOfStrings ( int n ) { int DP [ ] [ ] = new int [ n + 1 ] [ 6 ] ; DP [ 1 ] [ 1 ] = 1 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < 6 ; j ++ ) { if ( i == 1 ) { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + 1 ; } else { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + DP [ i - 1 ] [ j ] ; } } } return DP [ n ] [ 5 ] ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( findNumberOfStrings ( N ) ) ; } }