Weshall
BAN USERpublic class Jumble {
static class Node {
Node(int val) {
this.val =val;
}
int val;
Node next;
}
static void connect(Node node) {
//single node return;
if(node.next == null) {
return;
}
//double node return;
if(node.next.next == null) {
return;
}
Node head = new Node(-1);
head.next = node;
Node prev = null;
while(node.next != null) {
prev = node;
node = node.next;
}
Node second = head.next.next;
node.next = second;
head.next.next = node;
prev.next = null;
}
static Node jumble(Node node) {
Node head = new Node(-1);
head.next = node;
do {
connect(node);
if(node.next != null) {
node = node.next.next;
}
} while(node != null && node.next != null);
return head.next;
}
public static void main(String... args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
Node n5 = new Node(5);
Node n6 = new Node(6);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
Node s = n1;
System.out.print("Before::");
while(n1 != null) {
System.out.print(n1.val + "->");
n1 = n1.next;
}
jumble(s);
System.out.println();
System.out.print("After:: ");
while(s != null) {
System.out.print(s.val+ "->");
s = s.next;
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ManagerExp {
static void printHierarchy(Node node, String space) {
if(node == null) return;
System.out.println(space+""+node.name);
if(node.list != null) {
for (Node node1 : node.list) {
printHierarchy(node1, space+"-");
}
}
}
static class Node {
String name;
List<Node> list = new ArrayList();
Node(String name) {
this.name = name;
}
}
public static void main(String... args) {
List<Node> all = new ArrayList();
Node n = new Node("Manager1");
Node joe = new Node("Joe");
Node tom = new Node("Tom");
n.list.add(joe);
n.list.add(tom);
all.add(n);
Node linda = new Node("Linda");
Node potter = new Node("Potter");
joe.list.add(linda);
tom.list.add(potter);
Node harry = new Node("Harry");
linda.list.add(harry);
for(Node t : all) {
printHierarchy(t, "-");
}
}
}
import java.util.HashMap;
import java.util.Map;
public class PatternMatch {
public static void main(String... args) {
String[] strings = {"cat", "dog", "dog"};
String[] pattern = {"a", "b", "b"};
boolean matched = true;
if(strings.length != pattern.length) {
matched = false;
} else {
Map<String, String> map = new HashMap();
for (int i = 0; i < strings.length; i++) {
if(map.containsKey(strings[i])) {
if(!map.get(strings[i]).equals(pattern[i])) {
matched = false;
}
} else {
map.put(strings[i], pattern[i]);
}
}
}
if(matched) {
System.out.println("Matched pattern");
} else {
System.out.println("Pattern not found");
}
}
}
Slightly modified and compresses:
public class Deepest {
static class Node {
Node(Node parent, String x) {
this.parent = parent;this.x =x;
}
String x;
Node left;
Node right;
Node parent;
}
static Node getRoot(Node node) {
while(node.parent != null) {
node = node.parent;
}
return node;
}
static Node deep1(Node a1, Node a2) {
Node tmp = a1;
Node root = getRoot(tmp);
return common(root, a1.x, a2.x);
}
static Node common(Node root, String a, String b) {
if(root == null) return null;
if(root.x == a || root.x == b) {
return root;
}
Node lChild = common(root.left, a, b);
Node rChild = common(root.right, a, b);
if(lChild != null && rChild != null) {
return root;
}
return lChild != null ? lChild : rChild;
}
public static void main(String... args ) {
Node a = new Node(null, "A");
Node b = new Node(a, "B");
Node c = new Node(a, "C");
a.left = b;
a.right = c;
Node d = new Node(b, "D");
Node e = new Node(b, "E");
b.left = d;
b.right = e;
Node h = new Node(c, "H");
c.right = h;
Node g = new Node(d, "G");
Node f = new Node(d, "F");
d.right = f;
d.left = g;
System.out.println(deep1(d,f).x);
System.out.println(deep1(c,g).x);
System.out.println(deep1(e,b).x);
}
}
public class Deepest {
static class Node {
Node(Node parent, String x) {
this.parent = parent;this.x =x;
}
String x;
Node left;
Node right;
Node parent;
}
static Node deep(Node a1, Node a2) {
LinkedList<Node> first = new LinkedList<>();
while(a1 != null) {
first.add(a1);
a1 = a1.parent;
}
LinkedList<Node> second = new LinkedList<>();
while(a2 != null) {
second.add(a2);
a2 = a2.parent;
}
int c1 = first.size();
int c2 = second.size();
Node common = null;
while(c1 > 0 && c2 > 0) {
if(first.get(c1-1).x != second.get(c2-1).x ) {
common = first.get(c1);
break;
}
c2--;
c1--;
}
if(common == null) {
if(c1 == 0) {
return first.get(1);
}
if(c2 == 0) {
return second.get(1);
}
}
return common;
}
public static void main(String... args ) {
Node a = new Node(null, "A");
Node b = new Node(a, "B");
Node c = new Node(a, "C");
a.left = b;
a.right = c;
Node d = new Node(b, "D");
Node e = new Node(b, "E");
b.left = d;
b.right = e;
Node h = new Node(c, "H");
c.right = h;
Node g = new Node(d, "G");
Node f = new Node(d, "F");
d.right = f;
d.left = g;
System.out.println(deep(d,f).x);
System.out.println(deep(c,g).x);
System.out.println(deep(e,b).x);
}
}
- Weshall December 20, 2016