Spiga

ALGORITHM TO DETERMINE STRING CONTAINS UNIQUE CHARACTER

Implement an algorithm to determine if a string has all unique characters. With and without using extra additional data structures

/**
* Implement an algorithm to determine if a string has all unique characters.
* With and without using extra additional data structures?
*
@author Sonu Mishra
*/
public class StringContainsUniqueCharacter {
/**
* Without using extra DS.
*  Ignoring int variable. Time complexity is
* O(n), where n is the length of the string, and space complexity is O(n).
* We can reduce our space usage a little bit by using a bit vector.We will
* assume, in the below code, that the string is only lower case ‘a’ through
* ‘z’.This will allow us to use just a single int
*
@param str: String to check
@return : true/false
*/

Not Accessible Due To Restriction On Required Library

Not Accessible Due To Restriction On Required Library

Today, I was working on some java project in my favourite Eclipse editor, when I encountered some unknown problem. It was throwing some error that the calss cannot be accessed due to restriction on required library. This type of error occurs when some classes cannot be loaded into the projects since some restriction rules are being imposed on those classes. Similarly, I got the error like this:




How To solve this Exception:

As this error is coming because of some setting where we are excluding some files. So we need to change that settings. For that, click on the 'Windows' --> 'Preference' and then go to 'Java'-->Compiler-->'Error/Warnings' tab. You will find 'Forbidden reference (access rules):' and its value has been set to 'Error'. So we only need to do is, to change the 'Error' to 'Warning', and all is done.


Swapping the Variables Without Using Third Variables

Swapping the Variables Without Using Third Variables Using Java

In this post, we will see, how to swap the variables. There are different ways to swap the variables. The major swap algorithms are:
1) Swapping using the third varaibles.
2) Swapping using the Bitwise Operator. This is only for the Integer. This doesn't use the third variables.
3) Swapping using the Arithmetic Operations. This is also, only for the Integer. This also, doesn't use the
    third variables.
4) Swapping the Strings without using the third variables. This is little bit tricky. The logic is similar to the
    above one, i.e. Arithmetic Operations. But the Strings are immutable in the Java, so, externally we are not
    using the third variables, but internally, this uses the third variables.

Below, we have the Java Program to discuss all the above methods in details.

/**
* This class will reverse the two variables.
* Here we will see how to swap the variables using or without using the third variables.
* @author Sonu Mishra
*/
public class SwapVariables {
/**
* Swapping using the third variable and without the third variables.
* Swap two variables using the temporary variables or the third variable.
* @param a The first variable.
* @param b The Second variable.
*/

Basic TreeNode Structure in Java using Generics

Basic TreeNode Structure Using Generics

This is the basic block for creating the binary tree. We generally call this block or the structure as a Node. So this is how a single node is created. This in itself is a tree and when we pile the number of nodes, it become a big tree.

Here is the code, to create the TreeNode.

/**
* Basic Structure of the Tree. Or the Single unit of a tree.
* @author Sonu Mishra
* @param Type of the Node for the tree. Like. String, Integer
*/
TreeNode< E extends Comparable<? super E>>
{
public TreeNode< E > left;
public TreeNode< E > right;
E value;
public TreeNode(TreeNode< E > left, TreeNode< E > right, E value) {
this.left = left;
this.right = right;
this.value = value;
}
/**
* If a node doesn't have left and right child, it is leaf node.
* @param node check whether this node is leaf node or not.
* @return either true or false
*/

JAVA WAY TO WRITE TREE

JAVA WAY TO WRITE BINARY SEARCH TREE

A binary search tree (BST),is a node-based data structure in which each node has no more than two child nodes. Each child must either be a leaf node or the root of another binary search tree. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node.BST is also known as an ordered binary tree.
BST
Here is the Code to create a binary search tree in java.

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Stack;
/**
* Creates a Binary Search Tree.
* @author Sonu Mishra
*/
public class BinarySearchTree<E extends Comparable<? super E>>
{
private TreeNode<E> root = null;
public BinarySearchTree() {
root = new TreeNode(null, null, null);
}
public BinarySearchTree(E value) {
root = new TreeNode(null, null, value);
}
public void add(E value) {
addBefore(value, this.root);
}

How To Sort a File On the basis of the column Using Java

How To Sort a File On the basis of the column Using Java

Java Program to sort the file.
In the earlier post we have seen how to sort the CSV file. Now, we will see the generalised java code to sort the file on the basis of particular column. This code can be used for any type of files, like csv, txt etc. This can be useful to sort the big files on the basis of the column. For using this code, we need to take care of few things:
The format of the file should be like in this format:
The header or the column should be there and should be the first line of the file, else we will missed the first line.
Header/Col1: Header/Col2 : Header/Col3
a : c : e
s : a  : d
.. : .. : ...
Algorithm Used:
It’s not the rocket science, we have keep it simple and have used the simple algorithm. The each row has been considered as one java object. The column on which we have to sort is taken as the key and against this key we have the associated row data java objects as values. We sort the key and display the respective values.
For example: If we have to sort the above file on column 2, we will create two java objects which will have values:
Java@Obj1: a,c,e
Java@Obj2: s,a,d

Now, we are sorting on column 2, so for first row, key will be c and c will be mapped with the value Java@Obj1: a,c,e and for the second row: key will be a and a will be mapped with the value Java@Obj2: s,a,d. Now we will sort the keys, i.e. c and a and then we will display the mapped value for the sorted keys.

Here is the Java Class Example: