Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions solutions/java/RemoveDuplicatesFromString.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// Program to remove duplicates from a given string
// 1. getUniqueString(String) method uses a boolean array.
// 2. removeDuplicates(String) method uses a hash map.
// Both the methods have O(n) space and time complexity. (n being the string length)

import java.util.HashMap;

public class RemoveDuplicatesFromString {
public static void main(String[] args) {
RemoveDuplicatesFromString rsd = new RemoveDuplicatesFromString();
String input = "Tree Traversal";
System.out.println(rsd.getUniqueString(input));
System.out.println("Method 1: " +rsd.getUniqueString(input));
System.out.println("Method 2: " +rsd.removeDuplicates(input));
}

public String getUniqueString(String input) {
Expand All @@ -17,4 +25,17 @@ public String getUniqueString(String input) {
}
return sb.toString();
}
}

public String removeDuplicates(String input) {
HashMap<Character, Integer> map = new HashMap<>();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (!map.containsKey(c)) {
sb.append(c);
map.put(c, 1);
}
}
return sb.toString();
}
}
82 changes: 82 additions & 0 deletions solutions/java/TreeLevelOrderPrint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Program to create a Binary Search Tree and implement level order traversal.
// Author: Viveka Aggarwal

import java.util.LinkedList;
import java.util.Queue;

public class TreeLevelOrderPrint {
node root;
class node {
Integer value;
node left;
node right;

node() {
}

node (Integer value) {
this.value = value;
}
}

public TreeLevelOrderPrint() {
root = new node();
}

public treelevelorderprint(Integer value) {
root = new node(value);
}

public void addToTree(Integer value) {
if (root.value == null) {
root = new node(value);
} else {
addToTree(value, root);
}
}

public void addToTree(Integer value, node curr) {
if (value <= curr.value) {
if (curr.left == null)
curr.left = new node(value);
else
addToTree(value, curr.left);
} else {
if (curr.right == null)
curr.right = new node(value);
else
addToTree(value, curr.right);
}
}

public void levelOrder() {
if (root == null || root.value == null) {
System.out.println();
System.out.println("Empty tree!!!");
return;
}

Queue<node> q = new LinkedList<node>();
q.add(root);

while(!q.isEmpty()) {
node curr = q.poll();
System.out.print(curr.value + " ");

if(curr.left != null)
q.add(curr.left);

if(curr.right != null)
q.add(curr.right);
}
}

public static void main(String[] args) {
TreeLevelOrderPrint tree = new TreeLevelOrderPrint();
for (int i = 0; i <= 10; i++) {
tree.addToTree(i);
}
tree.addToTree(5);
tree.levelOrder();
}
}