Hashing And Different Techniques

What is hashing why we need it?

Consider it as a process of converting any input to an integer value.A string can have a hash value, a java Object can have hash value.

We need it to place these input to particular cell or bucket.So that whenever we need to find this object, we will find it in particular cell or bucket only.This will decrease the search time for an input object.

Click here to understand this part via youtube tutorial

Good Hashing Techniques

Since we need to convert the input value to some integer value, method or step to convert it should be simple and faster, as a user should not spend much time and effort on the secondary task.The second thing you should keep in mind is that your function should create different int value for different input for maximum cases.Otherwise, you will end up keeping elements in single or only a few buckets which will make search more problematic.

Two thing you have to learn is

  1. Event Distribution and Easy Computation
  2. Collision Detection and Resolution
  3. Collision resolution
    1. Linear Probing
    2. Quadratic Probing
    3. Double Hashing 

This is a minimum of hashing everyone should know.Will try to cover the hashing in depth in my upcoming blogs.

Given an array, find all the subsets

int arr[] = {1,2,3,4,5};
int level= 0;

for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
subset(arr[i]+””,i+1,arr);

}
}

private void subset(String i, int level,int[] arr) {

for (int j = level; j < arr.length; j++) {
System.out.println(i+””+arr[j]);
subset(i+””+arr[j],j+1,arr);

}

Find the all the lexical pair from the given array of strings.

lexical pairs

 

Vector<String> v = new Vector<String>();
Set<String> s = new HashSet<String>();
v.add(“Resistance”);
v.add(“Ancestries”);
v.add(“Gainly”);
v.add(“Laying”);
v.add(“test”);
v.add(“troop”);
v.add(“Amit”);
v.add(“Mait”);
v.add(“Self”);
v.add(“Taim”);
Boolean lexical = false,found = false;
System.out.println(“case 2”);

int lenght = v.size(),count = 0;

for (int i = 0; i < v.size(); i++) {
String source = v.get(i);

source = source.toLowerCase();
char[] sourceWord = source.toCharArray();
Arrays.sort(sourceWord);
source = new String(sourceWord);
//System.out.println(“source “+source);
count =0;
for (int j = i+1; j < v.size(); j++) {

String target = v.get(j);
target = target.toLowerCase();
char[] targetWord = target.toCharArray();
Arrays.sort(targetWord);
target = new String(targetWord);

if(source.equals(target)){
lexical = true;

System.out.print(v.get(i)+” “);
System.out.println(v.get(j));
continue;
// v.remove(v.get(j));

}

}
if(count>0)
System.out.println(“”);
}

if(!lexical){
System.out.println(“No such pairs”);
}

Given a matrix with each cell containing each number of candies, and a constraint that you can move only right or down, from the top left corner to the bottom right corner, find the path that gets you maximum candies.

Note: This result will print the all the possible path cost , you can fetch the path with maximum candies.

 

int matrix[][] = {{0,5,7},{7,9,1},{2,7,11}};
int rows = matrix.length,column = matrix[0].length;
System.out.println(“rows : “+rows );
System.out.println(“columns : “+column);
int TotalSum = 0,sum=0;

FindTheTotalSum(0,0,sum,rows,column,matrix);

}

private void FindTheTotalSum(int i, int j, int sum,int rows,int column,int[][] arr) {

if((i==column-1)&&(j==rows-1)){
System.out.println(sum+” sum of the path”);
}
sum = sum + arr[j][i];
if(i<column-1){
FindTheTotalSum(i+1, j, sum, rows, column,arr);
}
if(j<rows-1){
FindTheTotalSum(i, j+1, sum, rows, column,arr);
}

}

Stack implementation Algorithm

Stack Implementation

It works on first in last Out.

Youtube Video to understand Stack

 

Variables

top =0;

add to stack(push)
//boundary value:- if stack has a limit;
if(top<limit){
top ++;
add value to top
}else{
stack full
}

remove (pop)
if(top !=0){

take value from top
top–
}else{
stack Empty
}

Traversal

in order of first in

for (i = 1;i<=top;i++){
print element at i
}

 

WikiPedia Link  , refer for stack theory.

http://en.wikipedia.org/wiki/Stack_(abstract_data_type)

 

Rotate the matrix by 90 degree in java

We are rotating the content of a matrix by 90 degree.

Assumptions

a is the given matrix.

We will just print it in the rotated form.

///code

int a[][] = {{1,2,3,4,5,6},{7,8,9,10,11,12}};

//no. of column
int m = a[0].length;

//no. of rows
int n = a.length;

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
for (int i = 0; i < m; i++) {
for (int j = n-1; j >=0; j–) {
System.out.print(a[j][i]+” .. “);
}
System.out.println();
}

How to check if string can be used to create a palindrome

This is a code to check if we can make a palindrome using all letters of string

Input: str is a given string.

NOTE: assumption every character have to be use present in the string, and only once.

Logic: only one alphabet can have odd occurence,rest should have even occurence.

 

ALGO:

 

public static void main(String asd[]){

 

int count= 0;

int odd_occurence = 0;

//str is given string.you can initialize str with ant input you want.

String str =  “apapqqqqjjk”;

Boolean output = true;

for(int i =0;i<str.length();i++){

                for(int j=0;j 1)

                {

                                output = false;

                                break;

                }

}

 

if(output){

                system.out.println(“palindrome can be created”);

}else{

                system.out.println(“palindrome cannot be created”);

}

 

}