tasneem
BAN USER//Levenshtein Algo
public static void main(String [] args) {
String a [] = {"tasneem@gmail.com", "tas@hotmail.com", "abc@gmail.com", "abc@hotmail.com", "apc@yahoo.com"};
HashMap<Integer, ArrayList<String>> similarElementsMap = new HashMap<>();
for (int i = 0 ; i < a.length - 1; i ++) {
int d = distance(a[i], a[i+1]);
if (similarElementsMap.containsKey(d)) {
ArrayList<String> list = similarElementsMap.get(d);
list.add(a[i]);
list.add(a[i+1]);
} else {
ArrayList<String> list = new ArrayList<>();
list.add(a[i]);
list.add(a[i+1]);
similarElementsMap.put(d, list);
}
}
for (Entry<Integer, ArrayList<String>> map : similarElementsMap.entrySet()) {
System.out.println("For distance : "+map.getKey());
for (int i = 0 ; i < map.getValue().size(); i ++) {
System.out.println(map.getValue().get(i));
}
}
}
private static int distance(String a, String b) {
a = a.substring(0, a.indexOf('@')).toLowerCase();
b = b.substring(0, b.indexOf('@')).toLowerCase();
// base case
if (a.length() == 0) return b.length();
if (b.length() == 0)return a.length();
int cost[] = new int[b.length() + 1];
// Initialise cost with each indexes initialy
for (int i = 0 ; i < b.length(); i ++) {
cost[i] = i;
}
// Iterate through a and b string and update cost array with each a iteration
for (int i = 1; i <= a.length(); i ++) {
cost[0] = i;
int k = i - 1;
for (int j =1; j <= b.length(); j ++) {
int c = Math.min(1 + Math.min(cost[j], cost[j-1]), a.charAt(i-1) == b.charAt(j-1) ? k : k + 1);
// Swap the min value with cost and k
k = cost[j];
cost[j] = c;
}
}
return cost[b.length()];
}
- tasneem March 30, 2017private static void robinKarp() {
long time = System.currentTimeMillis();
String stream = "acacabcatghhellomvnsdb";
String arr [] = {"aca", "cat", "hello", "world", "sdb", "db", "b"};
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
String word = arr[i];
int l = word.length();
long currentHash = hash(word, l);
map.put(word, 0);
long hash = 0L;
for (int j = 0; j <= stream.length() - l ; j ++) {
hash = hash(stream, j , hash, l);
if (hash == currentHash) {
map.put(word, map.get(word) + 1);
}
}
System.out.println("robinKarp : "+ word + " :" + map.get(word));
}
System.out.println("robinKarp Time : "+(System.currentTimeMillis() - time));
}
private static long hash(String str, int length) {
long values = 0L;
for (int i = 0; i < length ; i ++) {
values = values + str.charAt(i) * (long)Math.pow(101, (length - 1 - i));
}
return values;
}
private static long hash(String str, int position, long oldHash, int lenght) {
if (position == 0 || oldHash == 0) {
return hash(str, lenght);
} else {
return (oldHash - str.charAt(position - 1) * (long)Math.pow(101, lenght - 1)) * 101 + str.charAt(position + lenght - 1);
}
}
- tasneem March 18, 2017public static void main(String[] args) {
String stream = "acacabcatghhellomvnsdb";
String arr [] = {"aca", "cat", "hello", "world", "sdb", "db", "b"};
HashMap<String, Integer> map = new HashMap<>();
if (stream == null) return;
for (int i = 0; i < arr.length; i++ ) {
String word = arr[i];
if (word == null) continue;
map.put(word, 0);
if (word.length() > stream.length()) continue;
if (word.length() == stream.length() && word.equals(stream)) {
map.put(word, 1);
continue;
}
for (int j = 0; j <= stream.length() - word.length(); j++) {
if (stream.charAt(j) == word.charAt(0)) {
if (stream.substring(j, j+word.length()).equals(word)) map.put(word, map.get(word) + 1);
}
}
System.out.println(word + " :" + map.get(word));
}
}
- tasneem March 18, 2017Use LinkedHashSet
public static void main(String[] args) {
int a [] = {1,2,4,5,7,8,9,9,4,1,1};
LinkedHashSet<Integer> set = new LinkedHashSet<>();
for (int i = 0; i < a.length; i ++ ) {
set.add(a[i]);
}
for (Integer integer : set) {
System.out.println(integer);
}
}
- tasneem March 15, 2017public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
int limit = scan.nextInt();
scan.close();
if (str.length() > limit ) {
str = str.substring(0, limit+1);
try {
if (!str.endsWith(" ")) {
str = str.substring(0, str.lastIndexOf(" "));
}
} catch (StringIndexOutOfBoundsException e) {
str = "";
}
}
System.out.println("Output "+str);
}
- tasneem March 15, 2017Using HashMap in O(n)
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < a.length; i++) {
if (map.containsKey(a[i])) {
map.put(a[i], map.get(a[i]) + 1);
} else {
map.put(a[i], 1);
}
}
for (int i = 0 ; i < b.length; i ++) {
if (map.containsKey(b[i]) && map.get(b[i]) > 0) {
System.out.println(b[i]);
map.put(b[i], map.get(b[i]) - 1);
}
public static void main (String[] args) throws java.lang.Exception {
String arr [] = {"aca", "hello", "world", "cat", "sdb", "acacabcatghhellomvnsdb"};
String input = "acacabcatghhellomvnsdb";
HashMap<String, Integer> map = new HashMap<>();
// put all words into map - O(l)
for (int i = 0 ; i < arr.length; i ++) {
map.put(arr[i], 0);
int len = arr[i].length();
int j = input.indexOf(arr[i].charAt(0));
while (j < input.length()) {
try {
String str = input.substring(j, len+j);
if (str.equals(arr[i])) {
map.put(str, map.get(str) + 1);
}
} catch(StringIndexOutOfBoundsException e) {
}
j++;
}
}
for (int i = 0; i < arr.length; i ++) {
System.out.println(arr[i] + " : " + map.get(arr[i]));
}
}
- tasneem April 01, 2017