george.maggessy
BAN USERpublic static void stringIntoSentence(String seq, Set<String> dict) {
StringBuffer sb = new StringBuffer();
if (seq != null && seq.length() > 0 && dict != null && !dict.isEmpty()) {
while (seq.length() > 0) {
String helper = "";
String foundIt = "";
char[] _seq = seq.toCharArray();
for (int i = 0; i < _seq.length; i++) {
helper += String.valueOf(_seq[i]);
if (dict.contains(helper))
foundIt = helper;
}
if (foundIt.length() > 0) {
seq = seq.substring(foundIt.length());
sb.append(foundIt);
if (seq.length() > 0)
sb.append(" ");
} else return;
}
}
System.out.println("--> " + sb.toString());
}
/**
* Given a Tuple for eg. (a, b, c)..
* Output : (*, *, *), (*, *, c), (*, b, *), (*, b, c), (a, *, *), (a, *, c), (a, b, *), (a, b, c)
*/
public static void buildTuple(String[] tuple) {
for (int i=0; i<powerN(2,tuple.length); i++) {
String b = toBinary(i, tuple.length);
System.out.print("(");
for (int j=0; j<b.length(); j++) {
if (b.substring(j,j+1).equals("1"))System.out.print(" "+tuple[j]+" ");
else System.out.print(" * ");
}
System.out.print(") ");
}
}
private static String toBinary(int base, int length) {
String b = Integer.toBinaryString(base);
for (int i = b.length(); i < length; i++) {
b = "0"+b;
}
return b;
}
private static int powerN(int base, int n) {
int result = 1;
for (int i = 0; i < n; i++) {
result *= base;
}
return result;
}
boolean isBST(TreeNode node) {
if (node == null) return true;
isBST(node.left);
isBST(node.right);
return (node.left==null?true:node.data > node.left.data)
&&
(node.right==null?true:node.data < node.right.data);
}
public void q1(String str) {
if (str==null || str.length()==0) return;
String[] s = str.split(" ");
int[] l = new int[s.length];
for (int i=0; i<s.length; i++) {
int h = l[Integer.valueOf(s[i])-1];
l[Integer.valueOf(s[i])-1] = (h==0?1:h+1);
}
for (int i=0; i<l.length; i++) {
if (l[i]>0)
System.out.print((i+1)+":"+l[i]+" ");
}
}
public class RomanNumeral {
public int getDecimal(char c) {
switch(c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
public int convert(String roman) {
char[] n = roman.toCharArray();
int total = 0;
char previous = n[n.length-1];
for (int i = n.length-1; i>=0; i--) {
int p = getDecimal(previous);
int c = getDecimal(n[i]);
if (p > c) total-=c;
else {
total+=c;
previous = n[i];
}
}
return total;
}
public static void main(String args[]) {
RomanNumeral rn = new RomanNumeral();
System.out.println(rn.convert("XXIV"));
}
}
public class StackGetMin {
private static final int MAX_SIZE = 100;
int[] stack = new int [MAX_SIZE];
int[] stackMin = new int [MAX_SIZE];
int min = Integer.MAX_VALUE;
int pos = -1;
void push(int value) {
if (pos > (MAX_SIZE-1)) return;
pos++;
stack[pos] = value;
if (value < min) min = value;
stackMin[pos] = min;
}
int pop() {
if (pos<0) return Integer.MIN_VALUE;
int popped = stack[pos];
stack[pos] = 0;
stackMin[pos] = 0;
pos--;
return popped;
}
int getMin() {
return stackMin[pos];
}
public static String replaceSpace(String source, String replacer) {
for (int i=0; i<source.length(); i++) {
if (source.charAt(i)==' ') {
source = source.substring(0,i)+replacer+source.substring(i+1);
i+=replacer.length()-1; //Minus 1 since it will add again on the next loop
}
}
return source;
}
public void bfs() {
// BFS uses Queue data structure
Queue queue = new LinkedList();
queue.add(this.rootNode);
System.out.println(this.rootNode);
visited.put(rootNode, true);
while (!queue.isEmpty()) {
Node node = (Node) queue.remove();
Node child = null;
while ((child = getUnvisitedChildNode(node)) != null) {
visited.put(child, true);
System.out.println(child);
queue.add(child);
}
}
// Clear visited property of nodes
clearNodes();
}
public void bfsRecursive(Queue queue) {
// BFS uses Queue data structure
if (queue.isEmpty()) return;
Node node = (Node) queue.remove();
System.out.println(node);
visited.put(node, true);
for (Node child: node.child) {
queue.add(child);
}
bfsRecursive(queue);
}
public void dfs() {
// DFS uses Stack data structure
Stack stack = new Stack();
stack.push(this.rootNode);
visited.put(rootNode, true);
System.out.println(rootNode);
while (!stack.isEmpty()) {
Node node = (Node) stack.peek();
Node child = getUnvisitedChildNode(node);
if (child != null) {
visited.put(child, true);
System.out.println(child);
stack.push(child);
} else {
stack.pop();
}
}
clearNodes();
}
public void dfsRecursive(Stack stack) {
// DFS uses Stack data structure
if (stack.empty()) return;
Node node = (Node) stack.peek();
if (visited.get(node)==null) System.out.println(node);
visited.put(node, true);
Node child = getUnvisitedChildNode(node);
if (child != null) {
stack.push(child);
} else {
stack.pop();
}
dfsRecursive(stack);
}
public static String removeChar(String input, char remove) {
String[] array = input.split(String.valueOf(remove));
String newString = "";
for (String a: array) {
newString+=a;
}
return newString;
}
public static int countSubstring(String input, String find) {
Pattern p = Pattern.compile(find);
Matcher m = p.matcher(input);
int i = 0;
while (m.find()) {
i++;
}
return i;
}
Denormalization avoids JOINs between tables, improving performance. The downside is the increase in space utilization.
- george.maggessy April 06, 2013Denormalization avoids JOINs between tables, improving performance. The downside is the increase in space utilization.
- george.maggessy April 06, 2013
Just tested it and it works.
- george.maggessy September 19, 2014public static void main(String[] args) {
// TODO code application logic here
Set<String> dict = new HashSet<String>();
dict.add("a");
dict.add("aa");
dict.add("aaa");
dict.add("is");
dict.add("name");
stringIntoSentence("aaaaisname", dict);
}