-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BFS/DFS tree, regex and digitsum
- Loading branch information
Showing
18 changed files
with
663 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.godepth.Anagrams; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] args){ | ||
Anagrams anagrams = new Anagrams(); | ||
anagrams.delete(); | ||
} | ||
} | ||
|
||
class Anagrams { | ||
|
||
public void delete() | ||
{ | ||
String leftRemoveString = ""; | ||
String rightRemoveString = ""; | ||
String anagramString = ""; | ||
String leftString = "cdesvsdctykyuwcfdvdfv"; | ||
String rightString = "abchdfbrbrtbvervwdveasxfdbthwc"; | ||
|
||
Map<String, String> left = new HashMap<>(); | ||
left.put("a", "0"); | ||
left.put("b", "0"); | ||
left.put("c", "0"); | ||
left.put("d", "0"); | ||
left.put("e", "0"); | ||
left.put("f", "0"); | ||
left.put("g", "0"); | ||
left.put("h", "0"); | ||
left.put("i", "0"); | ||
left.put("j", "0"); | ||
left.put("k", "0"); | ||
left.put("l", "0"); | ||
left.put("m", "0"); | ||
left.put("n", "0"); | ||
left.put("o", "0"); | ||
left.put("p", "0"); | ||
left.put("q", "0"); | ||
left.put("r", "0"); | ||
left.put("s", "0"); | ||
left.put("t", "0"); | ||
left.put("u", "0"); | ||
left.put("v", "0"); | ||
left.put("w", "0"); | ||
left.put("x", "0"); | ||
left.put("y", "0"); | ||
left.put("z", "0"); | ||
|
||
Map<String, String> right = new HashMap<>(); | ||
right.put("a", "0"); | ||
right.put("b", "0"); | ||
right.put("c", "0"); | ||
right.put("d", "0"); | ||
right.put("e", "0"); | ||
right.put("f", "0"); | ||
right.put("g", "0"); | ||
right.put("h", "0"); | ||
right.put("i", "0"); | ||
right.put("j", "0"); | ||
right.put("k", "0"); | ||
right.put("l", "0"); | ||
right.put("m", "0"); | ||
right.put("n", "0"); | ||
right.put("o", "0"); | ||
right.put("p", "0"); | ||
right.put("q", "0"); | ||
right.put("r", "0"); | ||
right.put("s", "0"); | ||
right.put("t", "0"); | ||
right.put("u", "0"); | ||
right.put("v", "0"); | ||
right.put("w", "0"); | ||
right.put("x", "0"); | ||
right.put("y", "0"); | ||
right.put("z", "0"); | ||
|
||
for (char item : leftString.toCharArray()) { | ||
left.put(Character.toString(item), Integer.toString(Integer.parseInt(left.get(Character.toString(item))) + 1)); | ||
System.out.println(Character.toString(item) + ":" + left.get(Character.toString(item))); | ||
} | ||
|
||
System.out.println("====================================================="); | ||
|
||
for (char item : rightString.toCharArray()) { | ||
right.put(Character.toString(item), Integer.toString(Integer.parseInt(right.get(Character.toString(item))) + 1)); | ||
System.out.println(Character.toString(item) + ":" + right.get(Character.toString(item))); | ||
} | ||
|
||
String list[] = {"a","b","c", "d", "e" , "f", "g", "h" , "i", "j", "k" , "l", "m", "n" , "o", "p", "q" , "r", "s", "t" , "u", "v", "w" , "x", "y", "z"}; | ||
|
||
for (String element : list) { | ||
if (Integer.parseInt(left.get(element)) >= 1 && Integer.parseInt(right.get(element)) >= 1) { | ||
anagramString += element; | ||
} else if (Integer.parseInt(left.get(element)) >= 1 && Integer.parseInt(right.get(element)) == 0) { | ||
leftRemoveString += element; | ||
} else if (Integer.parseInt(left.get(element)) == 0 && Integer.parseInt(right.get(element)) >= 1) { | ||
rightRemoveString += element; | ||
} | ||
} | ||
|
||
System.out.println("Anagrams: " + anagramString); | ||
System.out.println("Anagrams remove chars (left): " + leftRemoveString); | ||
System.out.println("Anagrams remove char number (left): " + leftRemoveString.length()); | ||
System.out.println("Anagrams remove chars (right): " + rightRemoveString); | ||
System.out.println("Anagrams remove char number (right): " + rightRemoveString.length()); | ||
System.out.println("Anagrams remove char number (total): " + ((leftRemoveString.length()) + (rightRemoveString.length()))); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.godepth.BFS; | ||
|
||
public class BFSNode<T> { | ||
private T data = null; | ||
private BFSNode<T> left, right; | ||
|
||
public BFSNode(T item) { | ||
data = item; | ||
left = right = null; | ||
} | ||
|
||
public BFSNode<T> getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(BFSNode<T> node) { | ||
left = node; | ||
} | ||
|
||
public BFSNode<T> getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(BFSNode<T> node) { | ||
right = node; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T item) { | ||
data = item; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.godepth.BFS; | ||
|
||
public class BFSTree<T> { | ||
public BFSNode<T> root; | ||
|
||
public BFSTree() { | ||
root = null; | ||
} | ||
|
||
public void printLevelOrder() { | ||
int height, i; | ||
height = height(root); | ||
|
||
for (i = 1; i <= height; i++) { | ||
printGivenLevel(root, i); | ||
} | ||
} | ||
|
||
private int height(BFSNode<T> root) { | ||
if (root == null) { | ||
return 0; | ||
} else { | ||
int leftHeight = height(root.getLeft()); | ||
int rightHeight = height(root.getRight()); | ||
return ((leftHeight > rightHeight ? leftHeight : rightHeight) + 1); | ||
} | ||
} | ||
|
||
private void printGivenLevel(BFSNode<T> root, int level) { | ||
if (root == null) { | ||
return; | ||
} | ||
|
||
if (level == 1) { | ||
System.out.print(root.getData() + " "); | ||
} else if (level > 1) { | ||
printGivenLevel(root.getLeft(), level - 1); | ||
printGivenLevel(root.getRight(), level - 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.godepth.BFS; | ||
|
||
import java.util.Queue; | ||
import java.util.LinkedList; | ||
|
||
public class QueueBFSTree<T> { | ||
|
||
public BFSNode<T> root; | ||
|
||
public void printLevelOrder() | ||
{ | ||
Queue<BFSNode<T>> queue = new LinkedList<>(); | ||
queue.add(root); | ||
|
||
while (!queue.isEmpty()) { | ||
|
||
BFSNode<T> temp = queue.poll(); | ||
System.out.print(temp.getData()); | ||
|
||
if (temp.getLeft() != null) { | ||
queue.add(temp.getLeft()); | ||
} | ||
|
||
if (temp.getRight() != null) { | ||
queue.add(temp.getRight()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.godepth.DFS; | ||
|
||
public class DFSNode<T> { | ||
private T data = null; | ||
private DFSNode<T> left, right; | ||
|
||
public DFSNode(T item) { | ||
data = item; | ||
left = right = null; | ||
} | ||
|
||
public DFSNode<T> getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(DFSNode<T> node) { | ||
left = node; | ||
} | ||
|
||
public DFSNode<T> getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(DFSNode<T> node) { | ||
right = node; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T item) { | ||
data = item; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.godepth.DFS; | ||
|
||
public class DFSTraversal<T> { | ||
|
||
public DFSNode<T> root; | ||
|
||
public void printInorder(DFSNode<T> node) | ||
{ | ||
if (node == null) | ||
return; | ||
|
||
printInorder(node.getLeft()); | ||
|
||
System.out.print(node.getData() + " "); | ||
|
||
printInorder(node.getRight()); | ||
} | ||
|
||
public void printPreorder(DFSNode<T> node) | ||
{ | ||
if (node == null) | ||
return; | ||
|
||
System.out.print(node.getData() + " "); | ||
|
||
printPreorder(node.getLeft()); | ||
|
||
printPreorder(node.getRight()); | ||
} | ||
|
||
public void printPostorder(DFSNode<T> node) | ||
{ | ||
if (node == null) | ||
return; | ||
|
||
printPostorder(node.getLeft()); | ||
|
||
printPostorder(node.getRight()); | ||
|
||
System.out.print(node.getData() + " "); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.godepth.DFS; | ||
|
||
import java.util.Stack; | ||
|
||
public class StackDSTTree<T> { | ||
|
||
public DFSNode<T> root; | ||
|
||
public void printDepthOrder() | ||
{ | ||
Stack<DFSNode<T>> stack = new Stack<>(); | ||
stack.add(root); | ||
|
||
while (!stack.isEmpty()){ | ||
|
||
DFSNode<T> temp = stack.pop(); | ||
System.out.print(temp.getData()); | ||
|
||
if (temp.getLeft() != null) { | ||
stack.push(temp.getLeft()); | ||
} | ||
|
||
if (temp.getRight() != null) { | ||
stack.push(temp.getRight()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.godepth.DiagonalDifference; | ||
|
||
class AbsoluteDifference { | ||
|
||
private int[][] square = { | ||
{3}, | ||
{11, 2, 4}, | ||
{4, 5, 6}, | ||
{10, 8, -12} | ||
}; | ||
|
||
public int count() | ||
{ | ||
int length = square[0][0]; | ||
int i; | ||
int left = 0; | ||
int right = 0; | ||
|
||
for (i = 0; i < length ; i++) { | ||
left = left + square[i+1][i]; | ||
right = right + square[i+1][length-i-1]; | ||
} | ||
|
||
return Math.abs(left - right); | ||
} | ||
} | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
AbsoluteDifference diff = new AbsoluteDifference(); | ||
System.out.print("Disgonal diff absolute value is " + diff.count()); | ||
} | ||
} |
Oops, something went wrong.