From b9a831faa6f899305e04722317ef2619ac3102cb Mon Sep 17 00:00:00 2001 From: Himanshu Date: Sun, 20 Jun 2021 02:36:32 +0530 Subject: [PATCH 1/3] Added two DP problems --- DP/NumberOfSubsetWithGivenDifference.java | 64 ++++++++++++++++++++++ DP/TargetSumProblem.java | 65 +++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 DP/NumberOfSubsetWithGivenDifference.java create mode 100644 DP/TargetSumProblem.java diff --git a/DP/NumberOfSubsetWithGivenDifference.java b/DP/NumberOfSubsetWithGivenDifference.java new file mode 100644 index 00000000..33cf3535 --- /dev/null +++ b/DP/NumberOfSubsetWithGivenDifference.java @@ -0,0 +1,64 @@ + +import java.util.*; +public class NumberOfSubsetWithGivenDifference { + public static int CountSubset(int[] wt,int sum,int n) + { + int[][] dp = new int[n+1][sum+1]; + for(int i=0;i<=sum;i++) + { + dp[0][i]=0; + } + for(int i=0;i<=n;i++) + { + dp[i][0]=1; + } + for(int i=0;i<=n;i++) + { + for(int j=0;j<=sum;j++) + { + try + { + dp[i][j]=dp[i-1][j] + dp[i-1][j-wt[i-1]]; + } + catch(Exception e) + { + + } + } + } + for(int i=0;i<=n;i++) + { + for(int j=0;j<=sum;j++) + { + System.out.print(dp[i][j]+" "); + } + System.out.println(); + } + + return dp[n][sum]; + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int size; + System.out.println("Enter the number of elements in the array"); + size=sc.nextInt(); + int[] wt = new int[size]; + System.out.println("Enter the elements in the array"); + for(int i=0;i Date: Tue, 29 Jun 2021 02:48:01 +0530 Subject: [PATCH 2/3] Added 5 more DP problems --- DP/InterLeavingProblem.java | 93 ++++++++++++++++++++++ DP/LongestCommonSubsequence.java | 98 +++++++++++++++++++++++ DP/LongestIncresingSubsequence.java | 63 +++++++++++++++ DP/LongestRepeatingSubsequence.java | 117 ++++++++++++++++++++++++++++ DP/PatternMatching.java | 111 ++++++++++++++++++++++++++ 5 files changed, 482 insertions(+) create mode 100644 DP/InterLeavingProblem.java create mode 100644 DP/LongestCommonSubsequence.java create mode 100644 DP/LongestIncresingSubsequence.java create mode 100644 DP/LongestRepeatingSubsequence.java create mode 100644 DP/PatternMatching.java diff --git a/DP/InterLeavingProblem.java b/DP/InterLeavingProblem.java new file mode 100644 index 00000000..67fd6b26 --- /dev/null +++ b/DP/InterLeavingProblem.java @@ -0,0 +1,93 @@ + +package javaapplication37; +import java.util.*; + +public class JavaApplication37 { + public static boolean Check(String s1,String s2,String s3,int l1,int l2,int l3,int p1,int p2,int p3) + { + //System.out.println("l1,l2,l3,p1,p2,p3= "+l1+" "+l2+" "+l3+" "+p1+" "+p2+" "+p3); + if(p3==l3) + { + + if(l1==p1 && l2==p2) + { + return true; + } + else + { + return false; + } + } + + String key = Integer.toString(p1)+"*"+Integer.toString(p2); + + Map map = new HashMap<>(); + + if(map.get(key)!=null) + { + return map.get(key); + } + + if(p1==l1) + { + if(s2.charAt(p2)==s3.charAt(p3)) + { + map.put(key, Check(s1,s2,s3,l1,l2,l3,p1,p2+1,p3+1)); + return map.get(key); + } + else + { + return false; + } + } + if(p2==l2) + { + if(s1.charAt(p1)==s3.charAt(p3)) + { + map.put(key, Check(s1,s2,s3,l1,l2,l3,p1+1,p2,p3+1)); + return map.get(key); + } + else + { + return false; + } + } + boolean one,two; + one=two=false; + if(s1.charAt(p1)==s3.charAt(p3)) + { + one=Check(s1,s2,s3,l1,l2,l3,p1+1,p2,p3+1); + } + if(s2.charAt(p2)==s3.charAt(p3)) + { + one=Check(s1,s2,s3,l1,l2,l3,p1,p2+1,p3+1); + } + + map.put(key, one||two); + return map.get(key); + + + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Enter the string s1"); + String s1 = sc.nextLine(); + System.out.println("Enter the string s2"); + String s2 = sc.nextLine(); + System.out.println("Enter the string s3"); + String s3 = sc.nextLine(); + int l1=s1.length(); + int l2=s2.length(); + int l3=s3.length(); + if(l3==l1+l2) + { + System.out.println(Check(s1,s2,s3,l1,l2,l3,0,0,0)); + } + else + { + System.out.println(false); + } + } + +} diff --git a/DP/LongestCommonSubsequence.java b/DP/LongestCommonSubsequence.java new file mode 100644 index 00000000..f00fa7f6 --- /dev/null +++ b/DP/LongestCommonSubsequence.java @@ -0,0 +1,98 @@ + +package javaapplication38; +import java.util.*; + +public class JavaApplication38 { + + public static void LenghtOFSubsequence(String s1,String s2) + { + int r,c; + r=s1.length()+1; + c=s2.length()+1; + int[][] dp = new int[r][c]; + + + + for(int i=0;i0 && j>0) + { + if(s1.charAt(i-1)==s2.charAt(j-1)) + { + lcs[index-1]=s1.charAt(i-1); + i--; + j--; + index--; + } + else if(dp[i-1][j]>dp[i][j-1]) + { + i--; + } + else + { + j--; + } + } + System.out.println("The longest common subsequence is "); + for(i=0;lcs[i]!='\0';i++) + { + System.out.print(lcs[i]+" "); + } + + + + + + + + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + String s1; + String s2; + System.out.println("Enter the string 1"); + s1 = sc.nextLine(); + System.out.println("Enter the string 2"); + s2=sc.nextLine(); + //System.out.println("Length of the longest common susequence is"+LenghtOFSubsequence(s1,s2)); + LenghtOFSubsequence(s1,s2); + } + +} diff --git a/DP/LongestIncresingSubsequence.java b/DP/LongestIncresingSubsequence.java new file mode 100644 index 00000000..a6f860de --- /dev/null +++ b/DP/LongestIncresingSubsequence.java @@ -0,0 +1,63 @@ +package javaapplication43; +import java.util.*; +public class JavaApplication43 { + + public static int IncresingSubsequence(int[] arr) + { + int i=1; + int j=0; + int[] Lis = new int[arr.length]; + + + for(int k=0;karr[j]) + { + Lis[i]=Math.max(Lis[i], 1+Lis[j]); + j++; + } + else + { + j++; + } + } + } + for(i=0;i0 && j>0 && index >0) + { + if(s1.charAt(i-1)==s2.charAt(j-1) && i!=j) + { + lrs[index-1]=s1.charAt(i-1); + index--; + i--; + j--; + } + else if(dp[i-1][j]>dp[i][j-1]) + { + i--; + } + else + { + j--; + } + } + + + //Printing LRS + + for(i=0;lrs[i]!='\0';i++) + { + System.out.print(lrs[i]); + } + + System.out.println(); + + + + + + + + + + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + String s1; + System.out.println("Enter the string "); + s1=sc.nextLine(); + String s2 =s1; + LongestReapetinSubsequence(s1,s2); + } + +} diff --git a/DP/PatternMatching.java b/DP/PatternMatching.java new file mode 100644 index 00000000..329e3ad3 --- /dev/null +++ b/DP/PatternMatching.java @@ -0,0 +1,111 @@ + +package javaapplication42; +import java.util.*; + + +public class JavaApplication42 { + public static boolean PatternMatching(String s,String p) + { + int r = p.length()+1; + int c = s.length()+1; + + int[][] dp = new int [r][c]; + + for(int i=0;i0 && j>0 && index>0) + { + if(p.charAt(i-1)==s.charAt(j-1)) + { + lcs[index-1] = p.charAt(i-1); + i--; + j--; + index--; + } + else if(dp[i-1][j]>dp[i][j-1]) + { + i--; + } + else + { + j--; + } + } + + //Printing lcs + + for(i=0;lcs[i]!='\0';i++) + { + System.out.print(lcs[i]+" "); + } + System.out.println(); + + + for(i=0;i Date: Wed, 30 Jun 2021 00:47:01 +0530 Subject: [PATCH 3/3] Binary Tress Updated --- .../CheckIfTwoBinaryTreesAreIdentical.java | 114 ++++++++++++++++ ...ceBetweenEvenlevelAndOddLevelElements.java | 100 ++++++++++++++ BinaryTrees/GetLevelOfAnyNode.java | 117 +++++++++++++++++ BinaryTrees/LeftViewOfBinaryTree.java | 122 ++++++++++++++++++ 4 files changed, 453 insertions(+) create mode 100644 BinaryTrees/CheckIfTwoBinaryTreesAreIdentical.java create mode 100644 BinaryTrees/DifferenceBetweenEvenlevelAndOddLevelElements.java create mode 100644 BinaryTrees/GetLevelOfAnyNode.java create mode 100644 BinaryTrees/LeftViewOfBinaryTree.java diff --git a/BinaryTrees/CheckIfTwoBinaryTreesAreIdentical.java b/BinaryTrees/CheckIfTwoBinaryTreesAreIdentical.java new file mode 100644 index 00000000..7bd7a495 --- /dev/null +++ b/BinaryTrees/CheckIfTwoBinaryTreesAreIdentical.java @@ -0,0 +1,114 @@ + +package javaapplication34; +import java.util.*; + +class Node +{ + public int data; + public Node lchild; + public Node rchild; + + public Node() + { + this.lchild=this.rchild=null; + } + + @Override + public String toString() + { + return this.data +" "; + } +} + +class BinaryTree +{ + Scanner sc = new Scanner(System.in); + public Node root = new Node(); + Queue q= new LinkedList<>(); + + public BinaryTree() + { + this.root.lchild=this.root.rchild=null; + } + public void Insertion() + { + q.add(root); + int num; + while(!q.isEmpty()) + { + Node n = q.poll(); + System.out.println("Enter the left child of "+n.data); + num=sc.nextInt(); + if(num!=-1) + { + Node temp=new Node(); + temp.data=num; + temp.lchild=temp.rchild=null; + q.add(temp); + n.lchild=temp; + + } + System.out.println("Enter the right child of "+n.data); + num=sc.nextInt(); + if(num!=-1) + { + Node temp=new Node(); + temp.data=num; + temp.lchild=temp.rchild=null; + q.add(temp); + n.rchild=temp; + + } + } + } + public void PreOrder(Node root) + { + if(root!=null) + { + System.out.print(root.data+" "); + PreOrder(root.lchild); + PreOrder(root.rchild); + } + } + + public boolean isIdentical(Node r1,Node r2) + { + if(r1==null && r2==null) + { + return true; + } + if(r1==null || r2==null) + { + return false; + } + + return r1.data==r2.data && isIdentical(r1.lchild,r2.lchild) && isIdentical(r1.rchild,r2.rchild); + } + +} + + +public class JavaApplication34 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + BinaryTree bt1 = new BinaryTree(); + System.out.println("Enter the root node of hte first tree"); + bt1.root.data=sc.nextInt(); + bt1.Insertion(); + bt1.PreOrder(bt1.root); + System.out.println(); + System.out.println("Enter the root node of the second tree"); + BinaryTree bt2 = new BinaryTree(); + bt2.root.data=sc.nextInt(); + bt2.Insertion(); + bt2.PreOrder(bt2.root); + BinaryTree bt = new BinaryTree(); + System.out.println(); + System.out.println(bt.isIdentical(bt1.root, bt2.root)); + + + } + +} diff --git a/BinaryTrees/DifferenceBetweenEvenlevelAndOddLevelElements.java b/BinaryTrees/DifferenceBetweenEvenlevelAndOddLevelElements.java new file mode 100644 index 00000000..bfd1646e --- /dev/null +++ b/BinaryTrees/DifferenceBetweenEvenlevelAndOddLevelElements.java @@ -0,0 +1,100 @@ + +package differencebetweenevenlevelandoddlevelelements; +import java.util.*; + +class Node +{ + int data; + Node lchild; + Node rchild; + + public Node(int value) + { + this.data=value; + this.lchild=this.rchild=null; + } + public Node() + { + this.lchild=this.rchild=null; + } +} + +class BinaryTree +{ + Scanner sc = new Scanner(System.in); + Node p = new Node(); + Node root = new Node(); + Queue q; + + public BinaryTree() + { + q = new LinkedList<>(); + q.add(root); + } + public void Insert() + { + while(!q.isEmpty()) + { + p=q.poll(); + System.out.println("Enter the left chlid of "+p.data); + int num; + num = sc.nextInt(); + if(num !=-1) + { + Node r = new Node(num); + r.lchild=r.rchild=null; + q.add(r); + p.lchild=r; + } + System.out.println("Enter the right chlid of "+p.data); + + num = sc.nextInt(); + if(num !=-1) + { + Node r = new Node(num); + r.lchild=r.rchild=null; + q.add(r); + p.rchild=r; + } + } + } +} + +public class DifferenceBetweenEvenlevelAndOddLevelElements { + public static void Preorder(Node P) + { + if(P!=null) + { + System.out.print(P.data+" "); + Preorder(P.lchild); + Preorder(P.rchild); + } + } + public static int EvenOddDifference(Node P) + { + if(P==null) + { + return 0; + } + else + { + return P.data - EvenOddDifference(P.lchild)-EvenOddDifference(P.rchild); + } + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the root node"); + int num =sc.nextInt(); + BinaryTree bt = new BinaryTree(); + bt.root.data=num; + bt.Insert(); + System.out.println(); + System.out.println("Preorder"); + Preorder(bt.root); + System.out.println(); + System.out.println(EvenOddDifference(bt.root)); + + + } + +} diff --git a/BinaryTrees/GetLevelOfAnyNode.java b/BinaryTrees/GetLevelOfAnyNode.java new file mode 100644 index 00000000..2c2aa238 --- /dev/null +++ b/BinaryTrees/GetLevelOfAnyNode.java @@ -0,0 +1,117 @@ + +package javaapplication35; +import java.util.*; + +class Node +{ + public int data; + public Node lchild; + public Node rchild; + + public Node() + { + this.lchild=this.rchild=null; + } + + @Override + public String toString() + { + return this.data+" "; + } +} + +class BinaryTree +{ + Scanner sc = new Scanner(System.in); + public Node root = new Node(); + Queue q= new LinkedList<>(); + + public BinaryTree() + { + this.root.lchild=this.root.rchild=null; + } + public void Insertion() + { + q.add(root); + int num; + while(!q.isEmpty()) + { + Node n = q.poll(); + System.out.println("Enter the left child of "+n.data); + num=sc.nextInt(); + if(num!=-1) + { + Node temp=new Node(); + temp.data=num; + temp.lchild=temp.rchild=null; + q.add(temp); + n.lchild=temp; + + } + System.out.println("Enter the right child of "+n.data); + num=sc.nextInt(); + if(num!=-1) + { + Node temp=new Node(); + temp.data=num; + temp.lchild=temp.rchild=null; + q.add(temp); + n.rchild=temp; + + } + } + } + public void PreOrder(Node root) + { + if(root!=null) + { + System.out.print(root.data+" "); + PreOrder(root.lchild); + PreOrder(root.rchild); + } + } + public int FindLevel(Node root,int num,int level) + { + //System.out.println(root); + + if(root==null) + { + return 0; + } + if(root.data==num) + { + return level; + } + + int res = FindLevel(root.lchild,num,level+1); + if(res!=0) + { + return res; + } + + + res = FindLevel(root.rchild,num,level+1); + return res; + } +} + +public class JavaApplication35 { + + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + BinaryTree bt = new BinaryTree(); + System.out.println("Enter the root node"); + bt.root.data=sc.nextInt(); + bt.Insertion(); + bt.PreOrder(bt.root); + System.out.println(); + int num; + System.out.println("Enter the number whose level you want to know"); + num=sc.nextInt(); + System.out.println(bt.FindLevel(bt.root,num,0)); + + } + +} diff --git a/BinaryTrees/LeftViewOfBinaryTree.java b/BinaryTrees/LeftViewOfBinaryTree.java new file mode 100644 index 00000000..3bd11c43 --- /dev/null +++ b/BinaryTrees/LeftViewOfBinaryTree.java @@ -0,0 +1,122 @@ + +package leftviewofbinarytree; + +import java.util.*; + + +class Node +{ + int data; + Node lchild; + Node rchild; + + public Node(int data) + { + this.data=data; + this.lchild=this.rchild=null; + } + public Node() + { + this.lchild=this.rchild=null; + } + + @Override + public String toString() + { + return this.data+" "; + } + +} + +class BinaryTree +{ + static int maxLevel=0; + Scanner sc = new Scanner(System.in); + Node root=new Node(); + public BinaryTree() + { + + root.lchild=root.rchild=null; + } + + Queue q = new LinkedList<>(); + + public void Insertion() + { + q.add(root); + int num; + + while(!q.isEmpty()) + { + Node p = q.poll(); + System.out.println("Enter the left child of "+p.data); + num=sc.nextInt(); + if(num!=-1) + { + Node t = new Node(); + t.data=num; + t.lchild=t.rchild=null; + p.lchild=t; + q.add(t); + } + System.out.println("Enter the right child of "+p.data); + num=sc.nextInt(); + if(num!=-1) + { + Node t = new Node(); + t.data=num; + t.lchild=t.rchild=null; + p.rchild=t; + q.add(t); + + } + + } + } + +// public void Preorder(Node root) +// { +// if(root!=null) +// { +// System.out.print(root+" "); +// Preorder(root.lchild); +// Preorder(root.rchild); +// } +// } + public void Left(Node root,int level) + { + + + if(root==null) + { + return; + } + if(level>=maxLevel) + { + System.out.print(root.data+" "); + maxLevel=level; + } + + Left(root.lchild,level+1); + Left(root.rchild,level+1); + } +} + +public class LeftViewOfBinaryTree { + + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int num; + System.out.println("Enter the root node"); + BinaryTree bt = new BinaryTree(); + bt.root.data=sc.nextInt(); + bt.Insertion(); +// bt.Preorder(bt.root); + System.out.println(); + + bt.Left(bt.root,0); + + } + +}