Skip to content

Instantly share code, notes, and snippets.

@anil477
anil477 / fib.java
Created May 17, 2017 15:47
Write a function fib() that a takes an integer n, and returns the nth fibonacci number.
// N time and O(1)O(1) space
import java.util.Map;
import java.util.HashMap;
class Fibber {
Map<Integer, Integer> memo = new HashMap<Integer, Integer>();
public int fib(int n) {
@anil477
anil477 / lcp.java
Last active May 23, 2017 09:48
Longest Common Prefix
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.Iterator;
class Trie {
private class TrieNode {
Map<Character, TrieNode> children;
@anil477
anil477 / prefix.java
Created May 23, 2017 10:36
Given a dictionary of words and an input string, find the longest prefix of the string which is also a word in dictionary.
// http://www.geeksforgeeks.org/longest-prefix-matching-a-trie-based-solution-in-java/
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.Iterator;
class Trie {
private class TrieNode {
@anil477
anil477 / trie_insert_search.java
Created May 23, 2017 11:41
A very basic Trie implementation to insert and search. We also maintain the frequency of each word inserted in the lead node.
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.Iterator;
class Trie {
private class TrieNode {
Map<Character, TrieNode> children;
@anil477
anil477 / directory.java
Last active May 24, 2017 18:30
Implement a Phone Directory using Trie
// Java Program to Implement a Phone
// Directory Using Trie Data Structure
import java.util.*;
class TrieNode
{
// Each Trie Node contains a Map 'child'
// where each alphabet points to a Trie
// Node.
HashMap<Character,TrieNode> child;
@anil477
anil477 / StackBasic.java
Last active May 26, 2017 18:07
Basic Stack Implementation in Java
public class MyStackImpl {
 
    private int stackSize;
    private int[] stackArr;
    private int top;
 
    /**
     * constructor to create stack with size
     * @param size
     */
@anil477
anil477 / sort.java
Last active May 27, 2017 08:59
Sort a stack
import java.util.*;
class sort{
public static void main(String args[]) {
Stack<Integer> s = new Stack<Integer>();
s.push(10);
s.push(8);
s.push(11);
s.push(1);
s.push(3);
@anil477
anil477 / reverse.java
Last active May 27, 2017 09:11
Reverse a Stack using recursion
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(30);
stack.push(-5);
stack.push(18);
stack.push(14);
stack.push(-3);
@anil477
anil477 / QueueArrayCircular.java
Created May 28, 2017 14:10
Circulare Implementation For Queue Using Array
// A class to represent a queue
class Queue
{
int front, rear, size;
int capacity;
int array[];
public Queue(int n) {
capacity = n;
front = size = 0;
@anil477
anil477 / QueueLinkedList.java
Created May 28, 2017 15:06
Queue Using Linked List
// A linked list (LL) node to store a queue entry
class QNode
{
public int key;
public QNode next;
// constructor to create a new linked list node
public QNode(int key) {
this.key = key;
this.next = null;