diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index e30743c..9885be6 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -3,3 +3,6 @@
- When writing solution for a given problem add the problem statement above the class as comment
- Write test case for each of the problem statement and make sure it builds with Travis CI
- Add the code link in the readme.md file with problem description and the link to the problem solution
+
+
+ - Give a star to the repo
diff --git a/README.md b/README.md
index 3b62ac8..c74f7b5 100644
--- a/README.md
+++ b/README.md
@@ -13,14 +13,30 @@ Stop thinking start contributing to leetcode solution
- [FindAllAnagramsInString](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/FindAllAnagramsInString.java)
- [TransposeMatrix](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/TransposeMatrix.java)
- [LowerCase](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/LowerCase.java)
-
-
+- [HammingDistance](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/HammingDistance.java)
+- [MiddleElementInLinkedList](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/MiddleElement.java)
+- [JudgeCircle](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/JudgeCircle.java)
+- [Parentheses](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/Parentheses.java)
+- [PlusOne](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/PlusOne.java)
+- [MinStack](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/MinStack.java)
### Medium
+
- [ValidSudoku](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/ValidSudoku.java)
- [GroupAnagrams](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/GroupAnagrams.java)
+- [NumberOfIslands](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/NumberOfIslands.java)
+- [SearchA2DMatrixII](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/SearchA2DMatrixII.java)
+- [ValidSudoku](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/ValidSudoku.java)
+- [GroupAnagrams](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/GroupAnagrams.java)
+- [NumberOfIslands](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/NumberOfIslands.java)
+- [SuperUglyNumber](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/SuperUglyNumber.java)
+- [MaximumProductOfWordLengths](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/MaximumProductOfWordLengths.java)
+- [SummaryRanges](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/SummaryRanges.java)
+- [MinimumHeightTree](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/MinimumHeightTree.java)
+- [CloneGraph](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/CloneGraph.java)
+
### Hard
-- [SudokuSolver](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/hard/SudokuSolver.java)
+- [SudokuSolver](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/hard/SudokuSolver.java)
diff --git a/bin/README.md b/bin/README.md
new file mode 100644
index 0000000..32134ed
--- /dev/null
+++ b/bin/README.md
@@ -0,0 +1,31 @@
+[](https://travis-ci.org/pratikpalashikar/leetcode-solutions)
+
+# leetcode-solutions
+Stop thinking start contributing to leetcode solution
+
+
+## Solution Lists
+
+### Easy
+
+- [JewelsNStones](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/JewelsNStones.java)
+- [ValidAnagram](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/ValidAnagram.java)
+- [FindAllAnagramsInString](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/FindAllAnagramsInString.java)
+- [TransposeMatrix](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/TransposeMatrix.java)
+- [LowerCase](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/LowerCase.java)
+- [HammingDistance](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/HammingDistance.java)
+- [MiddleElementInLinkedList](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/MiddleElement.java)
+- [JudgeCircle](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/JudgeCircle.java)
+- [Parentheses](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/Parentheses.java)
+- [PlusOne](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/PlusOne.java)
+- [MinStack](https://github.com/pratikpalashikar/leetcode-solutions/blob/master/src/main/java/com/techmisal/easy/MinStack.java)
+
+### Medium
+
+- [ValidSudoku](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/ValidSudoku.java)
+- [GroupAnagrams](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/GroupAnagrams.java)
+- [NumberOfIslands](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/medium/NumberOfIslands.java)
+
+### Hard
+
+- [SudokuSolver](https://github.com/harmishlakhani/leetcode-solutions/blob/master/src/main/java/com/techmisal/hard/SudokuSolver.java)
diff --git a/pom.xml b/pom.xml
index 6e500a4..c35f742 100644
--- a/pom.xml
+++ b/pom.xml
@@ -15,6 +15,12 @@
junit
4.12
+
+ org.junit.jupiter
+ junit-jupiter-api
+ RELEASE
+ test
+
@@ -41,4 +47,4 @@
-
\ No newline at end of file
+
diff --git a/src/main/java/com/techmisal/easy/HammingDistance.java b/src/main/java/com/techmisal/easy/HammingDistance.java
new file mode 100644
index 0000000..045d80b
--- /dev/null
+++ b/src/main/java/com/techmisal/easy/HammingDistance.java
@@ -0,0 +1,45 @@
+package com.techmisal.easy;
+
+
+/*
+
+The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
+
+Given two integers x and y, calculate the Hamming distance.
+
+Note:
+0 ≤ x, y < 231.
+
+Example:
+
+Input: x = 1, y = 4
+
+Output: 2
+
+Explanation:
+1 (0 0 0 1)
+4 (0 1 0 0)
+ ↑ ↑
+
+The above arrows point to positions where the corresponding bits are different.
+
+*/
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class HammingDistance {
+
+ public int hammingDistance(int x, int y) {
+
+
+
+ return Integer.bitCount(x^y);
+
+
+ }
+
+
+
+
+}
diff --git a/src/main/java/com/techmisal/easy/JudgeCircle.java b/src/main/java/com/techmisal/easy/JudgeCircle.java
new file mode 100644
index 0000000..7d1080e
--- /dev/null
+++ b/src/main/java/com/techmisal/easy/JudgeCircle.java
@@ -0,0 +1,55 @@
+package com.techmisal.easy;
+
+/*
+
+ Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
+
+The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.
+
+Example 1:
+Input: "UD"
+Output: true
+Example 2:
+Input: "LL"
+Output: false
+
+
+ */
+
+
+
+public class JudgeCircle {
+
+
+ public boolean judgeCircleMethod(String moves) {
+
+
+
+ int X=0;
+ int Y=0;
+
+
+ for(int i=0;i Returns -3.
+minStack.pop();
+minStack.top(); --> Returns 0.
+minStack.getMin(); --> Returns -2.
+
+
+*/
+
+public class MinStack {
+
+ int min = Integer.MAX_VALUE;
+
+ public Stack stack = new Stack();
+
+
+ public void pop() {
+
+
+ if (stack.pop() == min) {
+
+ min = stack.pop();
+
+ }
+
+ }
+
+
+ public void push(int x) {
+
+ if (x <= min) {
+ stack.push(min);
+ min = x;
+ }
+
+ stack.push(x);
+
+ }
+
+
+ public int getMin() {
+
+ return min;
+ }
+
+
+ public int top() {
+
+ return stack.peek();
+ }
+}
diff --git a/src/main/java/com/techmisal/easy/Parentheses.java b/src/main/java/com/techmisal/easy/Parentheses.java
new file mode 100644
index 0000000..a74795f
--- /dev/null
+++ b/src/main/java/com/techmisal/easy/Parentheses.java
@@ -0,0 +1,101 @@
+package com.techmisal.easy;
+
+
+/*
+
+Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
+
+An input string is valid if:
+
+Open brackets must be closed by the same type of brackets.
+Open brackets must be closed in the correct order.
+Note that an empty string is also considered valid.
+
+
+Example 1:
+
+Input: "()"
+Output: true
+Example 2:
+
+Input: "()[]{}"
+Output: true
+Example 3:
+
+Input: "(]"
+Output: false
+Example 4:
+
+Input: "([)]"
+Output: false
+Example 5:
+
+Input: "{[]}"
+Output: true
+
+ */
+
+import java.util.Stack;
+
+public class Parentheses {
+
+
+
+ public boolean getValidParanthenses(String str){
+
+ Stack stack = new Stack<>();
+
+
+ int pushCount = 0, popCount=0;
+
+ if(str.isEmpty()){
+ return true;
+ }
+
+
+ for(int i=0;i= 0);
+
+
+ if (dividend == 1) {
+
+
+ int[] array = new int[arr.length + 1];
+ for (int i = arr.length - 1; i >= 0; i--) {
+ array[i + 1] = arr[i];
+ }
+
+ array[0] = dividend;
+
+ return array;
+ }
+
+ return arr;
+
+ }
+
+
+}
diff --git a/src/main/java/com/techmisal/easy/VerticalOrderTraverse.java b/src/main/java/com/techmisal/easy/VerticalOrderTraverse.java
new file mode 100644
index 0000000..bf15166
--- /dev/null
+++ b/src/main/java/com/techmisal/easy/VerticalOrderTraverse.java
@@ -0,0 +1,71 @@
+package com.techmisal.easy;
+
+import java.util.TreeMap;
+import java.util.Vector;
+import java.util.Map.Entry;
+
+
+ class Node
+ {
+ int key;
+ Node left;
+ Node right;
+
+ // Constructor
+ Node(int data)
+ {
+ key = data;
+ left = null;
+ right = null;
+ }
+ }
+
+ public class VerticalOrderTraverse
+ {
+
+ // Utility function to store vertical order in map 'm'
+ // 'dist' is horizontal distance of current node from root.
+ static void getVerticalOrder(Node root, int dist,
+ TreeMap> m)
+ {
+ // Base case
+ if(root == null)
+ return;
+
+ //get the vector list at dist
+ Vector get = m.get(dist);
+
+ // Store current node in map 'm'
+ if(get == null)
+ {
+ get = new Vector<>();
+ get.add(root.key);
+ }
+ else
+ get.add(root.key);
+
+ m.put(dist, get);
+
+ getVerticalOrder(root.left, dist-1, m);
+
+ getVerticalOrder(root.right, dist+1, m);
+ }
+
+ // The main function to print vertical oder of a binary tree
+ // with given root
+ static void printVerticalOrder(Node root)
+ {
+ // Create a map and store vertical oder in map using
+ // function getVerticalOrder()
+ TreeMap> m = new TreeMap<>();
+ int hd =0;
+ getVerticalOrder(root,hd,m);
+
+ // Traverse the map and print nodes at every horizontal distance
+ for (Entry> entry : m.entrySet())
+ {
+ System.out.println(entry.getValue());
+ }
+ }
+
+}
diff --git a/src/main/java/com/techmisal/easy/WiggleSort.java b/src/main/java/com/techmisal/easy/WiggleSort.java
new file mode 100644
index 0000000..aa0670f
--- /dev/null
+++ b/src/main/java/com/techmisal/easy/WiggleSort.java
@@ -0,0 +1,53 @@
+package com.techmisal.easy;
+
+/*
+Goal: sort a given array of integers so that every second number is greater than both of its adjacents
+
+Method: sort numbers first, then split and rearrange
+
+Fail-Case: if a wiggle sort is impossible e.g. {1, 1, 1}, return null
+*/
+
+
+public class WiggleSort {
+ public int[] sort(int[] array) {
+ if (array.length == 1) return array;
+
+ //sort array with bubblesort
+ for (int j = 0; j < array.length-1; j++) { //takes (arraylength-1) moves to get highest number from left to right (worst-case)
+ for (int i = 0; i < array.length-1; i++) {
+ if (array[i] > array[i+1]) {
+ swap(array, i, i+1);
+ }
+ }
+ }
+
+ int lowmid = (int)(array.length/2)-1;
+ //check failcase
+ if (array.length > 2) {
+ if (array[lowmid] == array[lowmid+1]) {
+ if (array.length % 2 == 0 || array.length % 2 == 1 && array[lowmid+2] == array[lowmid]) {
+ return null;
+ }
+ }
+ }
+
+ //rearrange
+ int[] narray = new int[array.length];
+ for (int i = 0; i < array.length; i++) {
+ if (i % 2 == 0) {
+ narray[i] = array[(int)(i / 2)];
+ } else {
+ narray[i] = array[lowmid + 1 + (int)(i / 2)];
+ }
+ }
+
+ return narray;
+ }
+
+ private void swap(int[] nums, int i, int j) {
+ int temp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = temp;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/techmisal/medium/CloneGraph.java b/src/main/java/com/techmisal/medium/CloneGraph.java
new file mode 100644
index 0000000..c6542c9
--- /dev/null
+++ b/src/main/java/com/techmisal/medium/CloneGraph.java
@@ -0,0 +1,55 @@
+package com.techmisal.medium;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph.
+ * Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.
+ *
+ * Note:
+ * 1. The number of nodes will be between 1 and 100.
+ * 2. The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.
+ * 3. Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.
+ * 4. You must return the copy of the given node as a reference to the cloned graph.
+ */
+public class CloneGraph {
+ // Definition for a Node.
+ public static class Node {
+ public int val;
+ public List neighbors;
+
+ public Node() {
+ }
+
+ public Node(int _val, List _neighbors) {
+ val = _val;
+ neighbors = _neighbors;
+ }
+ };
+
+ private HashMap clones = new HashMap<>();
+
+ public Node cloneGraph(Node node) {
+ clones.clear();
+ return cloneNode(node);
+ }
+
+ private Node cloneNode(Node node) {
+ if (clones.containsKey(node)) {
+ return clones.get(node);
+ }
+ Node clonedNode = new Node();
+ clones.put(node, clonedNode);
+
+ List clonedNeighbors = new ArrayList<>();
+ for (Node neighBor : node.neighbors) {
+ clonedNeighbors.add(cloneNode(neighBor));
+ }
+
+ clonedNode.val = node.val;
+ clonedNode.neighbors = clonedNeighbors;
+ return clonedNode;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/techmisal/medium/MaximumProductOfWordLengths.java b/src/main/java/com/techmisal/medium/MaximumProductOfWordLengths.java
new file mode 100644
index 0000000..735090b
--- /dev/null
+++ b/src/main/java/com/techmisal/medium/MaximumProductOfWordLengths.java
@@ -0,0 +1,56 @@
+package com.techmisal.medium;
+
+/*
+Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters.
+You may assume that each word will contain only lower case letters.
+If no such two words exist, return 0.
+
+Example 1:
+
+Input: ["abcw","baz","foo","bar","xtfn","abcdef"]
+Output: 16
+Explanation: The two words can be "abcw", "xtfn".
+
+Example 2:
+
+Input: ["a","ab","abc","d","cd","bcd","abcd"]
+Output: 4
+Explanation: The two words can be "ab", "cd".
+
+Example 3:
+
+Input: ["a","aa","aaa","aaaa"]
+Output: 0
+Explanation: No such pair of words.
+*/
+
+public class MaximumProductOfWordLengths {
+
+ public int calculate(String[] words) {
+ if (words == null || words.length <= 1) {
+ return 0;
+ }
+
+ int length = words.length;
+ int[] encodedWords = new int[length];
+
+ for (int i = 0; i < length; i++) {
+ String word = words[i];
+ for (int j = 0; j < word.length(); j++) {
+ char c = word.charAt(j);
+ encodedWords[i] |= (1 << (c - 'a'));
+ }
+ }
+
+ int maxLen = 0;
+ for (int i = 0; i < length; i++) {
+ for (int j = i + 1; j < length; j++) {
+ if ((encodedWords[i] & encodedWords[j]) == 0) {
+ maxLen = Math.max(maxLen, words[i].length() * words[j].length());
+ }
+ }
+ }
+
+ return maxLen;
+ }
+}
diff --git a/src/main/java/com/techmisal/medium/MinimumHeightTree.java b/src/main/java/com/techmisal/medium/MinimumHeightTree.java
new file mode 100644
index 0000000..10e1a20
--- /dev/null
+++ b/src/main/java/com/techmisal/medium/MinimumHeightTree.java
@@ -0,0 +1,55 @@
+package com.techmisal.medium;
+
+import java.util.*;
+
+public class MinimumHeightTree {
+ public List findMinHeightTrees(int n, int[][] edges) {
+ List result = new ArrayList<>();
+ if(n==0){
+ return result;
+ }
+ if(n==1){
+ result.add(0);
+ return result;
+ }
+
+ ArrayList> graph = new ArrayList<>();
+ for(int i=0; i());
+ }
+
+ for(int[] edge: edges){
+ graph.get(edge[0]).add(edge[1]);
+ graph.get(edge[1]).add(edge[0]);
+ }
+
+ LinkedList leaves = new LinkedList<>();
+ for(int i=0; i2){
+ n = n-leaves.size();
+
+ LinkedList newLeaves = new LinkedList<>();
+
+ for(int l: leaves){
+ int neighbor = graph.get(l).iterator().next();
+ graph.get(neighbor).remove(l);
+ if(graph.get(neighbor).size()==1){
+ newLeaves.add(neighbor);
+ }
+ }
+
+ leaves = newLeaves;
+ }
+
+ return leaves;
+ }
+}
diff --git a/src/main/java/com/techmisal/medium/NumberOfIslands.java b/src/main/java/com/techmisal/medium/NumberOfIslands.java
new file mode 100644
index 0000000..9241c9f
--- /dev/null
+++ b/src/main/java/com/techmisal/medium/NumberOfIslands.java
@@ -0,0 +1,63 @@
+package com.techmisal.medium;
+
+import java.util.Arrays;
+
+public class NumberOfIslands {
+
+ private static char isLand = '1';
+ private static int size = 1000;
+ private Boolean[][] islandsMemo = new Boolean[size][size];
+ private int xRange;
+ private int yRange;
+
+ public NumberOfIslands() {
+ for(int x=0;x= xRange);
+ Boolean isOutOfYBound = ( y < 0 ) || (y >= yRange);
+
+ if(isOutOfXBound || isOutOfYBound) return;
+
+ Boolean isWater = !isIsland(grid[x][y]);
+ Boolean isFoundBefore = !islandsMemo[x][y];
+ if(isWater || isFoundBefore) return;
+
+ islandsMemo[x][y] = false;
+ this.flood(x,y+1, grid);
+ this.flood(x,y-1, grid);
+ this.flood(x+1,y, grid);
+ this.flood(x-1,y, grid);
+ }
+
+ public int numIslands(char[][] grid) {
+
+ initSolution(grid);
+ int result = 0;
+
+ for(int x=0;x summaryRanges(int[] nums) {
+ List result = new ArrayList<>();
+
+ for (int i = 0; i < nums.length; i++) {
+ StringBuffer range = new StringBuffer();
+ int start = i;
+ while (i < nums.length - 1 && (nums[i + 1] - nums[i]) == 1) {
+ i++;
+ }
+ if (start == i) {
+ result.add(String.valueOf(nums[i]));
+ } else {
+ range.append(nums[start]);
+ range.append("->");
+ range.append(nums[i]);
+ result.add(range.toString());
+ }
+ }
+ return result;
+ }
+}
diff --git a/src/main/java/com/techmisal/medium/SuperUglyNumber.java b/src/main/java/com/techmisal/medium/SuperUglyNumber.java
new file mode 100644
index 0000000..2462307
--- /dev/null
+++ b/src/main/java/com/techmisal/medium/SuperUglyNumber.java
@@ -0,0 +1,34 @@
+package com.techmisal.medium;
+
+import java.util.HashSet;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.Set;
+
+public class SuperUglyNumber {
+
+ public int nthSuperUglyNumber(int n, int[] primes) {
+ if (n <= 0 || primes == null || primes.length == 0) {
+ return 0;
+ }
+
+ Queue queue = new PriorityQueue<>();
+ Set set = new HashSet<>();
+
+ queue.add(1L);
+ set.add(1L);
+
+ for (int i = 1; i < n; i++) {
+ Long head = queue.poll();
+ for (int prime : primes) {
+ Long next = head * prime;
+ if (!set.contains(next)) {
+ queue.add(next);
+ set.add(next);
+ }
+ }
+ }
+
+ return queue.peek().intValue();
+ }
+}
diff --git a/src/main/java/com/techmisal/medium/WiggleSortII.java b/src/main/java/com/techmisal/medium/WiggleSortII.java
new file mode 100644
index 0000000..e418bf3
--- /dev/null
+++ b/src/main/java/com/techmisal/medium/WiggleSortII.java
@@ -0,0 +1,53 @@
+package com.techmisal.easy;
+
+/*
+Goal: sort a given array of integers so that every second number is greater than both of its adjacents
+
+Method: sort numbers first, then split and rearrange
+
+Fail-Case: if a wiggle sort is impossible e.g. {1, 1, 1}, return null
+*/
+
+
+public class WiggleSortII {
+ public int[] sort(int[] array) {
+ if (array.length == 1) return array;
+
+ //sort array with bubblesort
+ for (int j = 0; j < array.length-1; j++) { //takes (arraylength-1) moves to get highest number from left to right (worst-case)
+ for (int i = 0; i < array.length-1; i++) {
+ if (array[i] > array[i+1]) {
+ swap(array, i, i+1);
+ }
+ }
+ }
+
+ int lowmid = (int)(array.length/2)-1;
+ //check failcase
+ if (array.length > 2) {
+ if (array[lowmid] == array[lowmid+1]) {
+ if (array.length % 2 == 0 || array.length % 2 == 1 && array[lowmid+2] == array[lowmid]) {
+ return null;
+ }
+ }
+ }
+
+ //rearrange
+ int[] narray = new int[array.length];
+ for (int i = 0; i < array.length; i++) {
+ if (i % 2 == 0) {
+ narray[i] = array[(int)(i / 2)];
+ } else {
+ narray[i] = array[lowmid + 1 + (int)(i / 2)];
+ }
+ }
+
+ return narray;
+ }
+
+ private void swap(int[] nums, int i, int j) {
+ int temp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = temp;
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/techmisal/easy/HammingDistanceTest.java b/src/test/java/com/techmisal/easy/HammingDistanceTest.java
new file mode 100644
index 0000000..77e79e5
--- /dev/null
+++ b/src/test/java/com/techmisal/easy/HammingDistanceTest.java
@@ -0,0 +1,19 @@
+package com.techmisal.easy;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class HammingDistanceTest {
+
+
+
+ private HammingDistance hammingDistance = new HammingDistance();
+
+ @Test
+ public void testHammingDistance(){
+
+ Assert.assertEquals(2,hammingDistance.hammingDistance(1,4));
+
+ }
+
+}
diff --git a/src/test/java/com/techmisal/easy/JudgeCircleTest.java b/src/test/java/com/techmisal/easy/JudgeCircleTest.java
new file mode 100644
index 0000000..4754340
--- /dev/null
+++ b/src/test/java/com/techmisal/easy/JudgeCircleTest.java
@@ -0,0 +1,20 @@
+package com.techmisal.easy;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JudgeCircleTest {
+
+ JudgeCircle jc =new JudgeCircle();
+
+
+ @Test
+ public void testJudgeCircle(){
+
+ Assert.assertEquals(true,jc.judgeCircleMethod("UD"));
+
+ }
+
+
+
+}
diff --git a/src/test/java/com/techmisal/easy/MiddleElementTest.java b/src/test/java/com/techmisal/easy/MiddleElementTest.java
new file mode 100644
index 0000000..0bf12d0
--- /dev/null
+++ b/src/test/java/com/techmisal/easy/MiddleElementTest.java
@@ -0,0 +1,24 @@
+package com.techmisal.easy;
+
+
+import org.junit.Assert;
+
+public class MiddleElementTest {
+
+
+ MiddleElement middleElement = new MiddleElement();
+
+ public void testMiddleElement() {
+
+ ListNode node = new ListNode(1);
+
+ node.next = new ListNode(2);
+ node.next.next = new ListNode(3);
+
+
+
+ Assert.assertEquals(2,middleElement.middleNode(node).val);
+
+ }
+
+}
diff --git a/src/test/java/com/techmisal/easy/MinStackTest.java b/src/test/java/com/techmisal/easy/MinStackTest.java
new file mode 100644
index 0000000..a86d7d5
--- /dev/null
+++ b/src/test/java/com/techmisal/easy/MinStackTest.java
@@ -0,0 +1,29 @@
+package com.techmisal.easy;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MinStackTest {
+
+ MinStack minStack = new MinStack();
+
+
+ @Test
+ public void testMinStack(){
+
+
+ minStack.push(12);
+ minStack.push(-20);
+
+
+ Assert.assertEquals(-20,minStack.getMin());
+
+
+ }
+
+
+
+}
+
+
+
diff --git a/src/test/java/com/techmisal/easy/ParenthesesTest.java b/src/test/java/com/techmisal/easy/ParenthesesTest.java
new file mode 100644
index 0000000..3bd170d
--- /dev/null
+++ b/src/test/java/com/techmisal/easy/ParenthesesTest.java
@@ -0,0 +1,27 @@
+package com.techmisal.easy;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ParenthesesTest {
+
+
+ Parentheses parentheses = new Parentheses();
+
+
+ @Test
+ public void testParentheses(){
+
+
+ Assert.assertTrue(parentheses.getValidParanthenses(""));
+ Assert.assertTrue(parentheses.getValidParanthenses("()()()[]{}{}{}"));
+ Assert.assertTrue(parentheses.getValidParanthenses("[({})]"));
+ Assert.assertFalse(parentheses.getValidParanthenses("{[)}"));
+ Assert.assertFalse(parentheses.getValidParanthenses("alksfjsklfjslkj"));
+ Assert.assertFalse(parentheses.getValidParanthenses("342441341341341"));
+
+
+ }
+
+
+}
diff --git a/src/test/java/com/techmisal/easy/PlusOneTest.java b/src/test/java/com/techmisal/easy/PlusOneTest.java
new file mode 100644
index 0000000..02bd524
--- /dev/null
+++ b/src/test/java/com/techmisal/easy/PlusOneTest.java
@@ -0,0 +1,25 @@
+package com.techmisal.easy;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PlusOneTest {
+
+ PlusOne one = new PlusOne();
+
+
+ @Test
+ public void testOnePlus(){
+
+ Assert.assertArrayEquals(new int[]{1,0,0,0},one.addOne(new int[]{9,9,9}));
+ Assert.assertArrayEquals(new int[]{1,2,4},one.addOne(new int[]{1,2,3}));
+ Assert.assertArrayEquals(new int[]{},one.addOne(new int[]{}));
+ Assert.assertArrayEquals(new int[]{0,0,1},one.addOne(new int[]{0,0,0}));
+ Assert.assertArrayEquals(new int[]{2,0,0},one.addOne(new int[]{1,9,9}));
+ Assert.assertArrayEquals(new int[]{1,0,0,0,0,0},one.addOne(new int[]{9,9,9,9,9}));
+
+
+ }
+
+
+}
diff --git a/src/test/java/com/techmisal/easy/WiggleSortTest.java b/src/test/java/com/techmisal/easy/WiggleSortTest.java
new file mode 100644
index 0000000..9fb6edf
--- /dev/null
+++ b/src/test/java/com/techmisal/easy/WiggleSortTest.java
@@ -0,0 +1,20 @@
+package com.techmisal.easy;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class WiggleSortTest {
+ private WiggleSort wigglesort = new WiggleSort();
+
+ @Test
+ public void testWiggleSort() {
+ int[] testcase2 = {1, 2, 1, 1, 2, 2}; //only one solution
+ int[] rescase2 = {1, 2, 1, 2, 1, 2};
+
+ int[] testcase3 = {1, 2, 2, 3}; //no solution
+ int[] rescase3 = null;
+
+ Assert.assertArrayEquals( rescase2, wigglesort.sort(testcase2) );
+ Assert.assertArrayEquals( rescase3, wigglesort.sort(testcase3) );
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/techmisal/medium/CloneGraphTest.java b/src/test/java/com/techmisal/medium/CloneGraphTest.java
new file mode 100644
index 0000000..f12a1b8
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/CloneGraphTest.java
@@ -0,0 +1,93 @@
+package com.techmisal.medium;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.techmisal.medium.CloneGraph.Node;
+
+import org.junit.Test;
+
+public class CloneGraphTest {
+ private CloneGraph cloner = new CloneGraph();
+
+ @Test
+ public void SingleNode() {
+ List neighbors = new ArrayList<>(0);
+ Node node = new Node(2, neighbors);
+ Node clone = cloner.cloneGraph(node);
+ assertFalse(node == clone);
+ assertTrue(clone.neighbors.size() == 0);
+ }
+
+ @Test
+ public void DoubleNode() {
+ List leftn = new ArrayList(1);
+ List rightn = new ArrayList(1);
+ Node left = new Node(5, leftn);
+ Node right = new Node(24, rightn);
+ leftn.add(right);
+ rightn.add(left);
+
+ Node leftclone = cloner.cloneGraph(left);
+ assertFalse(leftclone == left || leftclone == right);
+ assertTrue(leftclone.neighbors.size() == 1);
+ assertTrue(leftclone.val == left.val);
+
+ Node rightclone = leftclone.neighbors.get(0);
+ assertFalse(rightclone == right || rightclone == left);
+ assertTrue(rightclone.neighbors.size() == 1);
+ assertTrue(rightclone.neighbors.contains(leftclone));
+ assertTrue(rightclone.val == right.val);
+ }
+
+ @Test
+ public void Square() {
+ List tln = new ArrayList(2);
+ List trn = new ArrayList(2);
+ List brn = new ArrayList(2);
+ List bln = new ArrayList(2);
+ Node tl = new Node(1, tln);
+ Node tr = new Node(2, trn);
+ Node br = new Node(3, brn);
+ Node bl = new Node(4, bln);
+ tln.add(tr);
+ tln.add(bl);
+ trn.add(tl);
+ trn.add(br);
+ brn.add(tr);
+ brn.add(bl);
+ bln.add(tl);
+ bln.add(br);
+
+ Node clone = cloner.cloneGraph(tl);
+ assertFalse(clone == tl || clone == tr || clone == br || clone == bl);
+ assertTrue(clone.neighbors.size() == 2);
+ assertTrue(clone.val == tl.val);
+ Node n2 = clone.neighbors.get(1);
+
+ Node n0 = clone.neighbors.get(0);
+ assertFalse(n0 == tl || n0 == tr || n0 == br || n0 == bl);
+ assertTrue(n0.neighbors.size() == 2);
+ assertTrue(n0.neighbors.contains(clone));
+ boolean n0istopright = n0.val == tr.val;
+ assertTrue(n0istopright || n0.val == bl.val);
+ int next = (n0.neighbors.indexOf(clone) == 0) ? 1 : 0;
+
+ Node n1 = n0.neighbors.get(next);
+ assertFalse(n1 == tl || n1 == tr || n1 == br || n1 == bl);
+ assertTrue(n1.neighbors.size() == 2);
+ assertFalse(n1.neighbors.contains(clone));
+ assertTrue(n1.neighbors.contains(n0));
+ assertTrue(n1.neighbors.contains(n2));
+ assertTrue(n1.val == br.val);
+
+ assertFalse(n2 == tl || n2 == tr || n2 == br || n2 == bl);
+ assertTrue(n2.neighbors.size() == 2);
+ assertTrue(n2.neighbors.contains(clone));
+ assertTrue(n2.neighbors.contains(n1));
+ assertTrue(n0istopright && n2.val == bl.val || !n0istopright && n2.val == tr.val);
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/techmisal/medium/MaximumProductOfWordLengthsTest.java b/src/test/java/com/techmisal/medium/MaximumProductOfWordLengthsTest.java
new file mode 100644
index 0000000..04602cb
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/MaximumProductOfWordLengthsTest.java
@@ -0,0 +1,15 @@
+package com.techmisal.medium;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MaximumProductOfWordLengthsTest {
+ private MaximumProductOfWordLengths solver = new MaximumProductOfWordLengths();
+
+ @Test
+ public void test() {
+ Assert.assertEquals(0, solver.calculate(new String[] { "a", "aa", "aaa", "aaaa" }));
+ Assert.assertEquals(16, solver.calculate(new String[] { "abcw", "baz", "foo", "bar", "xtfn", "abcdef" }));
+ Assert.assertEquals(4, solver.calculate(new String[] { "a", "ab", "abc", "d", "cd", "bcd", "abcd" }));
+ }
+}
diff --git a/src/test/java/com/techmisal/medium/MinimumHeightTreeTest.java b/src/test/java/com/techmisal/medium/MinimumHeightTreeTest.java
new file mode 100644
index 0000000..bfe7214
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/MinimumHeightTreeTest.java
@@ -0,0 +1,75 @@
+package com.techmisal.medium;
+
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class MinimumHeightTreeTest {
+
+ private MinimumHeightTree solver = new MinimumHeightTree();
+
+ @Test
+ public void shouldReturnEmptyIfNoVertices() {
+ int[][] vertices = {};
+ Integer n = 0;
+
+ List actualResult = solver.findMinHeightTrees(n, vertices);
+ List expectedResult = new LinkedList();
+
+ Assert.assertEquals(expectedResult, actualResult);
+ }
+
+ @Test
+ public void shouldReturnZeroIfOnlyOneLeave() {
+ int[][] vertices = {{0, 1}};
+ Integer n = 1;
+
+ List actualResult = solver.findMinHeightTrees(n, vertices);
+ List expectedResult = new LinkedList();
+ expectedResult.add(0);
+
+ Assert.assertEquals(expectedResult, actualResult);
+ }
+
+ @Test
+ public void shouldReturnOne() {
+ int[][] vertices = {{1, 0}, {1, 2}, {1, 3}};
+ Integer n = 4;
+
+ List actualResult = solver.findMinHeightTrees(n, vertices);
+ List expectedResult = new LinkedList();
+ expectedResult.add(1);
+
+ Assert.assertEquals(expectedResult, actualResult);
+ }
+
+ @Test
+ public void shouldReturnOneAndThree() {
+ int[][] vertices = {{0, 3}, {1, 3}, {2, 3}, {4, 3}, {5, 4}};
+ Integer n = 6;
+
+ List actualResult = solver.findMinHeightTrees(n, vertices);
+ List expectedResult = new LinkedList();
+ expectedResult.add(3);
+ expectedResult.add(4);
+
+ Assert.assertEquals(expectedResult, actualResult);
+ }
+
+ @Test
+ public void addFailingTestCaseFromJudge() {
+ int n = 11;
+ int[][] vertices = {{0, 1}, {0, 2}, {2, 3}, {0, 4}, {2, 5}, {5, 6}, {3, 7}, {6, 8}, {8, 9}, {9, 10}};
+
+ List actualResult = solver.findMinHeightTrees(n, vertices);
+ List expectedResult = new LinkedList();
+ expectedResult.add(5);
+ expectedResult.add(6);
+
+ Assert.assertEquals(expectedResult, actualResult);
+ }
+
+}
diff --git a/src/test/java/com/techmisal/medium/NumberOfIslandsTest.java b/src/test/java/com/techmisal/medium/NumberOfIslandsTest.java
new file mode 100644
index 0000000..ded3378
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/NumberOfIslandsTest.java
@@ -0,0 +1,41 @@
+package com.techmisal.medium;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class NumberOfIslandsTest {
+ private NumberOfIslands solver = new NumberOfIslands();
+
+ @Test
+ public void shouldReturnZero() {
+ char[][] island = new char[0][0];
+ int actualResult = solver.numIslands(island);
+ int expectedResult = 0;
+ Assert.assertEquals(actualResult, expectedResult);
+ }
+
+ @Test
+ public void shouldReturnOne() {
+ char[][] island = new char[][]{
+ {'0', '0', '0'},
+ {'0', '0', '1'},
+ {'0', '1', '1'},
+ };
+ int actualResult = solver.numIslands(island);
+ int expectedResult = 1;
+ Assert.assertEquals(actualResult, expectedResult);
+ }
+
+ @Test
+ public void shouldReturnThree() {
+ char[][] island = new char[][]{
+ {'0', '0', '0', '1', '0', '1'},
+ {'0', '0', '1', '0', '0', '0'},
+ {'0', '1', '1', '0', '0', '0'},
+ };
+ int actualResult = solver.numIslands(island);
+ int expectedResult = 3;
+ Assert.assertEquals(actualResult, expectedResult);
+ }
+
+}
diff --git a/src/test/java/com/techmisal/medium/SearchA2DMatrixIITest.java b/src/test/java/com/techmisal/medium/SearchA2DMatrixIITest.java
new file mode 100644
index 0000000..5614ee7
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/SearchA2DMatrixIITest.java
@@ -0,0 +1,22 @@
+package com.techmisal.medium;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+public class SearchA2DMatrixIITest
+{
+
+ @Test
+ public void testSearchMatrix()
+ {
+ SearchA2DMatrixII a = new SearchA2DMatrixII();
+
+ int[][] matrix = new int[][]{
+ {5,1,3,2},
+ {1,6,9,3}
+ };
+ assertEquals(true, a.searchMatrix(matrix, 5));
+ assertEquals(false, a.searchMatrix(matrix, 0));
+ }
+
+}
diff --git a/src/test/java/com/techmisal/medium/SummaryRangesTest.java b/src/test/java/com/techmisal/medium/SummaryRangesTest.java
new file mode 100644
index 0000000..f99256f
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/SummaryRangesTest.java
@@ -0,0 +1,29 @@
+package com.techmisal.medium;
+
+import java.util.List;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SummaryRangesTest {
+
+ private SummaryRanges summaryRanges = new SummaryRanges();
+
+ @Test
+ public void testSummaryRanges1() {
+ int[] nums = {0, 1, 2, 4, 5, 7};
+ List actualResult = summaryRanges.summaryRanges(nums);
+ Assert.assertEquals("0->2", actualResult.get(0));
+ Assert.assertEquals("4->5", actualResult.get(1));
+ Assert.assertEquals("7", actualResult.get(2));
+ }
+
+ @Test
+ public void testSummaryRanges2() {
+ int[] nums = {0, 2, 3, 4, 6, 8, 9};
+ List actualResult = summaryRanges.summaryRanges(nums);
+ Assert.assertEquals("0", actualResult.get(0));
+ Assert.assertEquals("2->4", actualResult.get(1));
+ Assert.assertEquals("6", actualResult.get(2));
+ Assert.assertEquals("8->9", actualResult.get(3));
+ }
+}
diff --git a/src/test/java/com/techmisal/medium/SuperUglyNumberTest.java b/src/test/java/com/techmisal/medium/SuperUglyNumberTest.java
new file mode 100644
index 0000000..4d0d0b4
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/SuperUglyNumberTest.java
@@ -0,0 +1,16 @@
+package com.techmisal.medium;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SuperUglyNumberTest {
+
+ private SuperUglyNumber superUglyNumber = new SuperUglyNumber();
+
+ @Test
+ public void testSuperUglyNumber() {
+ int[] primes = {2, 7, 13, 19};
+ int actualResult = superUglyNumber.nthSuperUglyNumber(12, primes);
+ Assert.assertEquals(actualResult, 32);
+ }
+}
diff --git a/src/test/java/com/techmisal/medium/WiggleSortIITest.java b/src/test/java/com/techmisal/medium/WiggleSortIITest.java
new file mode 100644
index 0000000..384a095
--- /dev/null
+++ b/src/test/java/com/techmisal/medium/WiggleSortIITest.java
@@ -0,0 +1,20 @@
+package com.techmisal.easy;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class WiggleSortIITest {
+ private WiggleSort wigglesort = new WiggleSort();
+
+ @Test
+ public void testWiggleSort() {
+ int[] testcase2 = {1, 2, 1, 1, 2, 2}; //only one solution
+ int[] rescase2 = {1, 2, 1, 2, 1, 2};
+
+ int[] testcase3 = {1, 2, 2, 3}; //no solution
+ int[] rescase3 = null;
+
+ Assert.assertArrayEquals( rescase2, wigglesort.sort(testcase2) );
+ Assert.assertArrayEquals( rescase3, wigglesort.sort(testcase3) );
+ }
+}
\ No newline at end of file