Huffman coding and compression – Code

huffman coding

Huffman coding generates the prefix codes based on the frequencies of corresponding characters, and then compresses the data from the generated code. The explanation of Huffman coding and compression can be found in wiki. How to implement Huffman coding and compression in Java? We build frequency-sorted binary tree to encode …

Continue reading

Prefix to postfix (2 solutions) – Code

Prefix To Postfix

In mathematics expressions, there are infix, prefix and postfix notations. Infix notation is characterized by the placement of operators between operands. For example, the plus sign in 2 + 2. Prefix notation is a notation in which operators precede their operands, such as + 3 4. Postfix notation is a …

Continue reading

Shortest path from source to destination in matrix – Code

Shortest path in matrix java

Shortest path in matrix is to find the shortest distance from the the source to the destination. As you know, graph can be represented as adjacent matrix. Therefore, we can use the Breadth First Search algorithm in graph to solve this problem. BFS starts from the source node. It explores …

Continue reading

Find K closest points to origin (2 solutions) – code

Find K closest points to center java

What is K closest points problem? Find K closest points is to find closest points to the pointer(0,0) (it is called center or origin). The input k is to specify how many points you should return. Here we provide 2 solutions. The first one uses PriorityQueue. ProrityQueue is commonly used …

Continue reading

Find all subset of string in dictionary – code

Find all subset of string in dictionary java

Find all subset of string in dictionary is to find the subset of an input string that exists in dictionary. The dictionary contains one million words. For this question, the trick is to find subset, not substring. What’s the difference between substring and subset? The substring is contiguous sequence of …

Continue reading

Convert hierarchy to nested objects – code

Build status hierarchy object java

There are online tools that convert JSON file to objects. The same idea applies to “convert hierarchy to nested objects”. The syntax of nested object is obj.children[1].children[2]. “obj” is the top level object. Each nested object’s level maps with the level in the hierarchy list. You can access any object …

Continue reading

Find top trending topics in twitter last hour (2 solutions) – code

top trending topic

Find top trending topics last hour (last day, month, year) is to find the topics that were tweeted most for the given time. To start with, the tweet can be defined as class with topic string and timeStamp . To keep track the topic’s count, we can use a HashMap …

Continue reading

Bi-directional BFS and examples – Code

BiDirectional BFS

A bi-directional search is to start search from two ends simultaneously. The drive of this is to make the search efficient. BFS is short for Breadth First Search. The search starts from the source node. Then explores all its adjacent nodes before going to the next level adjacent nodes. The …

Continue reading

Java custom Comparators and examples

Java User defined comparators

Java custom Comparators are important to sort the user defined classes. Here we give examples on how to implement user defined Comparators and use them. They are in String, PriorityQueue and TreeMap. 1. Java custom Comparators for String When sorting strings, it is sorted by Alphabetical order by default. However …

Continue reading