-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6111296
commit 8e385d2
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(logn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |