From 8e385d229293786071f0c11b0295de7ff9616d04 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 6 Feb 2024 18:01:54 +0530 Subject: [PATCH] Added codes for 6 Feb --- GeeksForGeeks/February/6-2-24/GFG.java | 176 ++++++++++++++++++++++++ GeeksForGeeks/February/6-2-24/README.md | 2 + LeetCode/February/6-2-24/README.md | 2 + LeetCode/February/6-2-24/Solution.java | 17 +++ 4 files changed, 197 insertions(+) create mode 100644 GeeksForGeeks/February/6-2-24/GFG.java create mode 100644 GeeksForGeeks/February/6-2-24/README.md create mode 100644 LeetCode/February/6-2-24/README.md create mode 100644 LeetCode/February/6-2-24/Solution.java diff --git a/GeeksForGeeks/February/6-2-24/GFG.java b/GeeksForGeeks/February/6-2-24/GFG.java new file mode 100644 index 0000000..2e4c7db --- /dev/null +++ b/GeeksForGeeks/February/6-2-24/GFG.java @@ -0,0 +1,176 @@ +//{ Driver Code Starts +//Initial Template for Java + + +/*package whatever //do not write package name here */ + +import java.io.*; +import java.util.*; +import java.math.*; + +class Node +{ + int data; + Node left, right; + + public Node(int d) + { + data = d; + left = right = null; + } +} + +class GFG +{ + static Node buildTree(String str) + { + // Corner Case + if(str.length() == 0 || str.equals('N')) + return null; + String[] s = str.split(" "); + + Node root = new Node(Integer.parseInt(s[0])); + Queue q = new LinkedList(); + q.add(root); + + // Starting from the second element + int i = 1; + while(!q.isEmpty() && i < s.length) + { + // Get and remove the front of the queue + Node currrNode = q.remove(); + + // Get the currrent node's value from the string + String currrVal = s[i]; + + // If the left child is not null + if(!currrVal.equals("N")) + { + + // Create the left child for the currrent node + currrNode.left = new Node(Integer.parseInt(currrVal)); + + // Push it to the queue + q.add(currrNode.left); + } + + // For the right child + i++; + if(i >= s.length) + break; + currrVal = s[i]; + + // If the right child is not null + if(!currrVal.equals("N")) + { + + // Create the right child for the currrent node + currrNode.right = new Node(Integer.parseInt(currrVal)); + + // Push it to the queue + q.add(currrNode.right); + } + + i++; + } + + return root; + } + + public static void main(String args[]) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(br.readLine().trim()); + while(t>0) + { + String s = br.readLine(); + Node root = buildTree(s); + + int k = Integer.parseInt(br.readLine().trim()); + + Solution T = new Solution(); + System.out.println(T.printKDistantfromLeaf(root,k)); + t--; + } + } +} + + + +// } Driver Code Ends + + +//User function Template for Java + +// class Node +// { +// int data; +// Node left, right; + +// public Node(int d) +// { +// data = d; +// left = right = null; +// } +// } + +class Solution +{ + //Function to return count of nodes at a given distance from leaf nodes. + int printKDistantfromLeaf(Node root, int k) + { + if(root == null) + { + return 0; + } + + Queue q = new ArrayDeque<>(); + q.add(root); + + int c = 0; + while(!q.isEmpty()) + { + int size = q.size(); + while(size-- > 0) + { + Node curr = q.remove(); + if(solve(curr,k)) + { + c++; + } + if(curr.left != null) + { + q.add(curr.left); + } + if(curr.right != null) + { + q.add(curr.right); + } + } + } + + return c; + } + + + public static boolean solve(Node root,int k) + { + if(root == null) + { + return false; + } + if((root.left == null && root.right == null) && k == 0) + { + return true; + } + if((root.left == null && root.right == null) || k <= 0) + { + return false; + } + + boolean l = solve(root.left,k-1); + boolean r = solve(root.right,k-1); + + return l || r; + } +} diff --git a/GeeksForGeeks/February/6-2-24/README.md b/GeeksForGeeks/February/6-2-24/README.md new file mode 100644 index 0000000..743e668 --- /dev/null +++ b/GeeksForGeeks/February/6-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(logn) diff --git a/LeetCode/February/6-2-24/README.md b/LeetCode/February/6-2-24/README.md new file mode 100644 index 0000000..5aeb9b1 --- /dev/null +++ b/LeetCode/February/6-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*k*logk) , where n = |strs| and k = |strs[i]| +Space complexity - O(n*k) diff --git a/LeetCode/February/6-2-24/Solution.java b/LeetCode/February/6-2-24/Solution.java new file mode 100644 index 0000000..ef44668 --- /dev/null +++ b/LeetCode/February/6-2-24/Solution.java @@ -0,0 +1,17 @@ +class Solution +{ + public List> groupAnagrams(String[] strs) + { + Map> keyToAnagrams = new HashMap<>(); + + for (final String str : strs) + { + char[] chars = str.toCharArray(); + Arrays.sort(chars); + String key = String.valueOf(chars); + keyToAnagrams.computeIfAbsent(key, k -> new ArrayList<>()).add(str); + } + + return new ArrayList<>(keyToAnagrams.values()); + } +}