From 0a12e156e50c14a77f756d1eafdba6de6b6a7229 Mon Sep 17 00:00:00 2001 From: Viveka Date: Wed, 12 Aug 2015 10:44:05 -0400 Subject: [PATCH 1/3] Update RemoveDuplicatesFromString.java --- .../java/RemoveDuplicatesFromString.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/solutions/java/RemoveDuplicatesFromString.java b/solutions/java/RemoveDuplicatesFromString.java index 1dfe597..77ec01f 100644 --- a/solutions/java/RemoveDuplicatesFromString.java +++ b/solutions/java/RemoveDuplicatesFromString.java @@ -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) { @@ -17,4 +25,17 @@ public String getUniqueString(String input) { } return sb.toString(); } -} \ No newline at end of file + + public String removeDuplicates(String input) { + HashMap 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(); + } +} From 11fb93f3cb2c7c389e324b96a137ce303711fee9 Mon Sep 17 00:00:00 2001 From: Viveka Date: Wed, 12 Aug 2015 12:28:37 -0400 Subject: [PATCH 2/3] Create treelevelorderprint --- solutions/java/treelevelorderprint | 82 ++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 solutions/java/treelevelorderprint diff --git a/solutions/java/treelevelorderprint b/solutions/java/treelevelorderprint new file mode 100644 index 0000000..c6f5cf1 --- /dev/null +++ b/solutions/java/treelevelorderprint @@ -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 q = new LinkedList(); + 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(); + } +} From ed60d0e2cb71d5b4dff1b081d3deb2819ea4a2ea Mon Sep 17 00:00:00 2001 From: Viveka Date: Fri, 14 Aug 2015 16:26:27 -0400 Subject: [PATCH 3/3] Updated TreeLevelOrderPrint.java --- .../java/{treelevelorderprint => TreeLevelOrderPrint.java} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename solutions/java/{treelevelorderprint => TreeLevelOrderPrint.java} (92%) diff --git a/solutions/java/treelevelorderprint b/solutions/java/TreeLevelOrderPrint.java similarity index 92% rename from solutions/java/treelevelorderprint rename to solutions/java/TreeLevelOrderPrint.java index c6f5cf1..cdbab77 100644 --- a/solutions/java/treelevelorderprint +++ b/solutions/java/TreeLevelOrderPrint.java @@ -4,7 +4,7 @@ import java.util.LinkedList; import java.util.Queue; -public class treelevelorderprint { +public class TreeLevelOrderPrint { node root; class node { Integer value; @@ -19,7 +19,7 @@ class node { } } - public treelevelorderprint() { + public TreeLevelOrderPrint() { root = new node(); } @@ -72,7 +72,7 @@ public void levelOrder() { } public static void main(String[] args) { - treelevelorderprint tree = new treelevelorderprint(); + TreeLevelOrderPrint tree = new TreeLevelOrderPrint(); for (int i = 0; i <= 10; i++) { tree.addToTree(i); }