Skip to content

Commit

Permalink
Added codes for 6 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 6, 2024
1 parent 6111296 commit 8e385d2
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
176 changes: 176 additions & 0 deletions GeeksForGeeks/February/6-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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 <Node> q = new LinkedList<Node>();
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<Node> 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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/6-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(logn)
2 changes: 2 additions & 0 deletions LeetCode/February/6-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*k*logk) , where n = |strs| and k = |strs[i]|
Space complexity - O(n*k)
17 changes: 17 additions & 0 deletions LeetCode/February/6-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution
{
public List<List<String>> groupAnagrams(String[] strs)
{
Map<String, List<String>> 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());
}
}

0 comments on commit 8e385d2

Please sign in to comment.