bebert.dude
BAN USERSmall and simple (if regexp were allowed)
public static void main(String[] args)
{
Pattern p = Pattern.compile("(hh+ii+rr+ee+)+");
Matcher m = p.matcher("hhhhiirreehhiirree");
System.out.println(m.matches());
}
No in order successor to 6, and here is my solution.
public class BSTinorder
{
// Return the in order successor of a given node in a BTS
public class Node {
public Node right;
public Node left;
public float data;
public Node(float data, Node left, Node right) {
this.right = right;
this.left = left;
this.data = data;
}
}
private Node root = new Node(4,
new Node(2,
new Node(1, null, null),
new Node(3, null, null)
),
new Node(6,
new Node(6,
new Node(5,
new Node(4.5f, null, null),
new Node(5.5f, null, null)
),
null
),
null
)
);
public Node after(float value, Node node) {
if (value >= node.data) {
return node.right == null ? null : after(value, node.right);
} else {
if (node.left == null) {
return node;
}
Node afterLeft = after(value, node.left);
if (afterLeft == null) {
return node;
}
return node.data < afterLeft.data ? node : afterLeft;
}
}
public static void main(String[] args)
{
BSTinorder instance = new BSTinorder();
Node node = instance.after(Float.parseFloat(args[0]),instance.root);
if (node == null) {
System.out.println("no successor");
} else {
System.out.println(node.data);
}
}
}
Correct. I misread the input to n>=1 and that regexp would fail with hire
- bebert.dude October 25, 2013