Wednesday, September 3, 2014

Check if char in a string are unique


public class StringUniqueChar {
final static int TotalChar = 128; //Total Number of ASCII
public boolean isUnique(String s) {
if(s.length()>TotalChar) return false;
else {
boolean[] hash = new boolean[TotalChar];
for(int i=0; i<s.length(); i++) {
int possition = s.charAt(i);
if(hash[possition]==true) return false;
else hash[possition] = true;
}
return true;
}
}
}

Simple Linked List Using Java

public class List {
List next;
int data;

List(int d) {
data = d;
next = null;
}

void appendData(int d) {
List l = new List(d);
List n = this;
while(n.next != null) {
n = n.next; 
}
n.next = l;
}

void printData() {
List n = this;
for(; n.next != null; n = n.next)
System.out.println(n.data);
System.out.println(n.data);
System.out.println("-END-");
}

void appendMiddle(int d, int position) {
List n = this;
for(int i=1; i<position; i++)
n=n.next;
List l = new List(d);
l.next = n.next;
n.next = l;
}

void deleteEnd() {
List n = this;
for(; n.next.next!=null; n=n.next) {}
n.next = null;
}

void deleteMiddle(int possition) {
List n = this;
for(int i=1; i<possition; i++)
n=n.next;
List a = n.next;
n.next = n.next.next;
a = null;
}
}
UA-39217154-2