Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views4 pages

Java Coding Solutions

The document discusses three solutions for hashing problems. The first solution uses horizontal distance to solve the bottom view of a binary tree problem. The second solution finds a pair in an array that sums to a target number using a hash map. The third solution sorts and returns a string based on character frequencies using a priority queue.

Uploaded by

sahilsharan2704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Java Coding Solutions

The document discusses three solutions for hashing problems. The first solution uses horizontal distance to solve the bottom view of a binary tree problem. The second solution finds a pair in an array that sums to a target number using a hash map. The third solution sorts and returns a string based on character frequencies using a priority queue.

Uploaded by

sahilsharan2704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

HASHING SOLUTIONS

[email protected]
Solution 1:
Using the concept of Horizontal Distance (HD), as discussed in Top View of a Binary Tree
Question in the Trees chapter.

import java.util.*;

e e
class Solution {

er
static class Node {

h
int data;

e
int hd;

m
Node left, right;

ms g
public Node(int key) {

@
this.data = key;

d
this.hd = Integer.MAX_VALUE;

I
this.left = this.right = null;

le
}

Te
}

public static void bottomViewHelper(Node root, int curr, int hd,


TreeMap<Integer, int[]> m) {
if (root == null)
return;

// If node for a particular HD is not present, add to the map.


if (!m.containsKey(hd)) {
m.put(hd, new int[]{ root.data, curr });
}

// Compare height for already


// present node at similar horizontal
// distance
else {
int[] p = m.get(hd);
if (p[1] <= curr) {
p[1] = curr;
p[0] = root.data;
}
m.put(hd, p);

[email protected]
}

// call for left subtree


bottomViewHelper(root.left, curr + 1, hd - 1, m);

// call for right subtree


bottomViewHelper(root.right, curr + 1, hd + 1, m);

e
}

er e
public static void printBottomView(Node root) {

eh
// Map to store Horizontal Distance, Height and Data.
TreeMap<Integer, int[]> m = new TreeMap<>();

s g m
bottomViewHelper(root, 0, 0, m);

@ m
// Prints the values stored by printBottomViewUtil()

I d
for(int val[] : m.values())

le
{

Te
System.out.print(val[0] + " ");
}
}

public static void main(String[] args) {


Node root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(5);
root.left.right = new Node(3);
root.right.left = new Node(4);
root.right.right = new Node(25);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);

System.out.println(
"Bottom view of the given binary tree:");

printBottomView(root);
}
}

[email protected]
Solution 2:

public int[] twoSum(int[] arr, int target) {


Map<Integer, Integer> visited = new HashMap<>();
for(int i = 0; i<arr.length; i++) {
//diff = given target - number given at ith index

e e
int diff = target - arr[i];

h er
// check if found difference is present in the MAP list

e
if(visited.containsKey(diff)) {

m
//if difference in map matches with the ith index element in array

s g
return new int[] { i, visited.get(diff) };

m
}

@
//add arr element in map to match with future element if forms a pair

d
visited.put(arr[i],i);

I
}

le
//if no matches are found

Te
return new int[] {0, 0};
}

Solution 3 :

public String frequencySort(String s) {


HashMap<Character , Integer>map = new HashMap<>();
for(int i=0;i<s.length();++i)
map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);

PriorityQueue<Map.Entry<Character,Integer>> pq =
new PriorityQueue<>((a,b)->a.getValue()== b.getValue()?
a.getKey()-b.getKey(): b.getValue()-a.getValue());

for(Map.Entry<Character,Integer>e:map.entrySet()) pq.add(e);

For Full course Dm @msgmeheree


StringBuilder res = new StringBuilder();
while(pq.size()!=0){
char ch = pq.poll().getKey();

[email protected]
int val = map.get(ch);
while(val!=0){
res.append(ch);
val--;
}
}
return res.toString();

e
}

er e
eh
s g m
@ m
le I d
Te

You might also like