From 02f55886d35a3dd0e58717f5a7ce539eb892d56d Mon Sep 17 00:00:00 2001 From: Richa Date: Tue, 13 Oct 2020 17:21:20 +0530 Subject: [PATCH] Added few more problems. --- CoinChange.java | 59 ++++++++++++++++++++++++++++++++++++++++ CountNumberOfHops.java | 47 ++++++++++++++++++++++++++++++++ DistinctOccurrences.java | 44 ++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 CoinChange.java create mode 100644 CountNumberOfHops.java create mode 100644 DistinctOccurrences.java diff --git a/CoinChange.java b/CoinChange.java new file mode 100644 index 0000000..0ea1628 --- /dev/null +++ b/CoinChange.java @@ -0,0 +1,59 @@ +/*You are given an amount denoted by value. You are also given an array of coins. The array contains the denominations + * of the give coins. You need to find the minimum number of coins to make the change for value using the coins of given + * denominations. Also, keep in mind that you have infinite supply of the coins. + * */ +//Initial Template for Java + +import java.io.*; +import java.util.*; +public class CoinChange { + public static void main (String[] args) { + Scanner sc=new Scanner(System.in); + int testcases=sc.nextInt(); + while(testcases-->0) + { + int value=sc.nextInt(); + int numberOfCoins=sc.nextInt(); + int coins[]=new int[numberOfCoins]; + + for(int i=0;iamount?-1:dp[amount]; + } +} + + +// { Driver Code Starts. + + // } Driver Code Ends \ No newline at end of file diff --git a/CountNumberOfHops.java b/CountNumberOfHops.java new file mode 100644 index 0000000..01ff1bf --- /dev/null +++ b/CountNumberOfHops.java @@ -0,0 +1,47 @@ +/*A frog jumps either 1, 2, or 3 steps to go to the top. In how many ways can it reach the top. As the answer will be + * large find the answer modulo 1000000007. + */ +//Initial Template for Java + +import java.util.*; +import java.io.*; +import java.lang.*; + +public class CountNumberOfHops +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + + while(t-- > 0) + { + int n = sc.nextInt(); + System.out.println(new Hops().countWays(n)); + + } + } +} +// } Driver Code Ends + + +//User function Template for Java + + +class Hops +{ + static long countWays(int n) + { + if(n==0||n==1) + return 1; + long dp[]=new long[n+1]; + dp[0]=1; + dp[1]=1; + dp[2]=2; + for(int i=3;i<=n;i++) + dp[i]=(dp[i-1]+dp[i-2]+dp[i-3])%1000000007; + return dp[n]; + } + +} + diff --git a/DistinctOccurrences.java b/DistinctOccurrences.java new file mode 100644 index 0000000..76fb5b0 --- /dev/null +++ b/DistinctOccurrences.java @@ -0,0 +1,44 @@ +/*Given two strings S and T of length n and m respectively. find count of distinct occurrences of T in S as a sub-sequence. + */ +import java.util.*; + +public class DistinctOccurrences +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + sc.nextLine(); + while(t>0) + { + String line = sc.nextLine(); + String S = line.split(" ")[0]; + String T = line.split(" ")[1]; + + Solution sol=new Solution(); + System.out.println(sol.subsequenceCount(S,T)); + t--; + } + } +}// } Driver Code Ends + + +/*You are required to complete this method*/ +class Solution +{ + int subsequenceCount(String s, String t) + { + int dp[][]=new int[t.length()+1][s.length()+1]; + for(int j=0;j<=s.length();j++) + dp[0][j]=1; + for(int i=0;i