Sunday, February 19, 2017

Lexicographic Order String in Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LexigrophicOrder {

public static void main(String[] args) {

String input = "abcd";
boolean loopCond = true;
String lexiographicString = "bacd";
List<Character> list = new ArrayList<Character>(input.length());
for (int i = 0; i < input.length(); i++) {
list.add(input.charAt(i));
}
Collections.sort(list);
int k = 0;
int orderNum = 0;
while (loopCond) {
if (list.size() >= 1) {
for (int i = 0; i < list.size(); i++) {
if (lexiographicString.charAt(k) == list.get(i)) {
orderNum += i * getNum(lexiographicString.length() - k - 1);
list.remove(i);
k++;
break;
}
}
} else {
loopCond = false;
}
}
System.out.println(orderNum+1);
}

private static int getNum(int num) {

int count = 1;

for (int i = 1; i <= num; i++) {
count *= i;
}
return count;

}

}

Saturday, November 19, 2016

Dictionary Implementation with Trie. Pattern Search will work for 355k words

import java.util.HashMap;

public class TrieNode
{
    private char character;
   
    private HashMap<Character, TrieNode> children;
   
    private boolean isEnd;
   
    TrieNode(char character)
    {
        this.character = character;
        children = new HashMap<>();
        isEnd = false;
       
    }
   
    public char getCharacter()
    {
        return character;
    }
   
    public void setCharacter(char character)
    {
        this.character = character;
    }
   
    public HashMap<Character, TrieNode> getChildren()
    {
        return children;
    }
   
    public void setChildren(HashMap<Character, TrieNode> children)
    {
        this.children = children;
    }
   
    public boolean isEnd()
    {
        return isEnd;
    }
   
    public void setEnd(boolean isEnd)
    {
        this.isEnd = isEnd;
    }
}





import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;

public class TrieOperations
{
    private TrieNode root;
   
    private static int totalFoundWords;
   
    public TrieOperations()
    {
        root = new TrieNode((char)0);
        totalFoundWords =0;
    }
   
    public void insert(String word)
    {
        int length = word.length();
        TrieNode temp = root;
        for (int i = 0; i < length; i++)
        {
            char ch = word.charAt(i);
            HashMap<Character, TrieNode> child = temp.getChildren();
           
            if (child.containsKey(ch))
            {
                temp = child.get(ch);
            }
            else
            {
                TrieNode next = new TrieNode(ch);
                child.put(ch, next);
                temp = next;
            }
           
        }
        temp.setEnd(true);
    }
   
    public void patterMatching(String word)
    {
        int length = word.length();
        TrieNode temp = root;
        String value = "";
        boolean cont = false;
        for (int i = 0; i < length; i++)
        {
            char ch = word.charAt(i);
            HashMap<Character, TrieNode> child = temp.getChildren();
            if (child.containsKey(ch))
            {
                temp = child.get(ch);
                cont = true;
                value += ch;
            }
            else
            {
                cont = false;
                break;
            }
        }
        if (cont)
        {
            findPossibleStrings(value, temp);
        }
        System.out.println(totalFoundWords +" words are found");
       
    }
   
    private void findPossibleStrings(String value, TrieNode temp)
    {
        if (temp.isEnd())
        {
            System.out.println(value);
            totalFoundWords++;
        }
        HashMap<Character, TrieNode> child = temp.getChildren();
        for (Entry<Character, TrieNode> entry : child.entrySet())
        {
            value += entry.getKey();
            findPossibleStrings(value, entry.getValue());
            value = value.substring(0, value.length() - 1);
        }
    }
   
    public static void main(String[] args)
    {
        TrieOperations opr = new TrieOperations();
        //you can get the list of words from https://raw.githubusercontent.com/dwyl/english-words/master/words.txt
        File file = new File(Your list of words file);
        BufferedReader br = null;
        try
        {
            br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null)
            {
                opr.insert(line.trim());
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != br)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        String input = "prabhu";
        opr.patterMatching(input);
    }
}

Monday, October 24, 2016

Find a linkedlist is a palindrome or not

public class LinkNode {

@Override
public String toString() {
return "LinkNode [data=" + data + ", next=" + next + "]";
}

private int data;

private LinkNode next;

LinkNode() {
this.data = 0;
this.next = null;
}

LinkNode(int data, LinkNode next) {
this.data = data;
this.next = next;
}

}

public class Palindrome {
private static LinkNode node1 = null;

private static int front = -1;

private static int rear = -1;

private static int[] queue = new int[100];

public static void main(String[] args) {
int n1 = 12;
int rem = 0;
while (n1 != 0) {
rem = n1 % 10;
node1 = new Palindrome().insert(node1, rem);
n1 = n1 / 10;
}
System.out.println(new Palindrome().findPalindrome(node1));

}

private boolean findPalindrome(LinkNode node) {
if (null == node) {
return true;
}
insertqueue(node.getData());
boolean result = findPalindrome(node.getNext());
if (removequeue() == node.getData() && result) {
return true;
}
return false;
}

private LinkNode insert(LinkNode node, int data) {
if (null == node) {
node = new LinkNode(data, null);
return node;
}
LinkNode temp = node;
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(new LinkNode(data, null));
return node;
}

private int removequeue() {
return queue[front++];
}

private void insertqueue(int data) {
queue[++rear] = data;
if (front == -1) {
front = 0;
}
}

}

Tuesday, March 22, 2016

Find Nth Element From Last of the LinkedList in Single Pass

package LinkedList;

public class NthLastElementFromLast {

Nod head;
int size;

NthLastElementFromLast()
{
head = null;
size = 0;
}

public static void main(String args[])
{
NthLastElementFromLast elementFromLast = new NthLastElementFromLast();
elementFromLast.add(1);
elementFromLast.add(2);
elementFromLast.add(3);
elementFromLast.add(4);
elementFromLast.add(5);
elementFromLast.display();
elementFromLast.findNthFromLast(3);
}

void add(int val)
{
size++;
if(head == null)
{
head = new Nod(val,null);
}
else
{
Nod temp = new Nod(val,head);
head = temp;
}
}

void display()
{
Nod temp = head;
while(temp != null)
{
System.out.println(temp.data);
temp = temp.link;
}
}

void findNthFromLast(int n)
{
if(n>size || n<0)
return;
Nod temp = head;
Nod temp1 = head;
for(int i=0;i<n;i++)
temp = temp.link;
while(temp != null)
{
temp = temp.link;
temp1 = temp1.link;
}
System.out.println(temp1.data);

}

}

class Nod {
int data;
Nod link;
Nod()
{
data =0;
link = null;
}
Nod(int data,Nod link)
{
this.data = data;
this.link = link;
}
}

Reverse a LinkedList using recursion

public void reverse()
{
Node newNode = null;
head = method(head,newNode);
}

private Node method(Node old,Node newNode)
{
if(old == null)
{
return null;
}
else
{
Node temp = method(old.getLink(),newNode);
if(temp == null)
{
newNode = old;
return newNode;
}
else
{
newNode = temp;
old.setLink(null);
while(temp.getLink() != null)
{
temp = temp.getLink();
}
temp.setLink(old);
return newNode;
}
}
}

Lexicographic Rank of a String

public class LexicographicRank
{
    public static void main(String args[])
    {
        String str = "dcba";
        int n = str.length();
        int rank = 0;
        for (int i = 0; i < n - 1; i++)
        {
            int x = 0;
            for (int j = i + 1; j < n; j++)
            {
                if (str.charAt(i) > str.charAt(j))
                    x++;
            }
            rank = rank + (x * (fact(n - i - 1)));
        }
        System.out.println(rank + 1);
    }
   
    private static int fact(int n)
    {
        int res = 1;
        if (n == 0)
            return 1;
        else
        {
            for (int i = 1; i <= n; i++)
            {
                res = res * i;
            }
        }
        return res;
    }
}

Saturday, March 19, 2016

Selection Sort in Java

import java.util.stream.IntStream;

public class SelectionSort
{
    public static void main(String args[])
    {
        int[] arr = new int[] {5, 12, 1, -5, 16};
        new SelectionSort().sort(arr);
        IntStream.of(arr).forEach(System.out::println);
    }
   
    private void sort(int[] arr)
    {
        int index = 0, smallEle;
        for (int i = 0; i < arr.length - 1; i++)
        {
            smallEle = arr[i];
            for (int j = i + 1; j < arr.length; j++)
            {
                if (arr[j] < smallEle)
                {
                    index = j;
                    smallEle = arr[j];
                }
            }
            smallEle = arr[i];
            arr[i] = arr[index];
            arr[index] = smallEle;
        }
    }
}