rajeevkrishnanv
BAN USER1. select co.countryName from Country co where not exists (select 'X' from city ci where co.countryId=ci.countyId); ---- CountryNames whose does not have any cities
2. select co.countyName from Country co where 3>(select count(*) from city ci where ci.countryId=co.countryId);
Not tested
Using recursion :)
private static int sumArray(int a[], int pos) {
if(pos==a.length)
return 0;
return a[pos]+sumArray(a, pos+1);
}
Tested and working fine.... Tree used to get multiple possiblites and to handle ambiguity
package com.algorithm;
import java.util.TreeSet;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class SegmentWord {
private static TreeSet<String> dict = new TreeSet<String>();
static {
dict.add("this");
dict.add("is");
dict.add("awesome");
dict.add("awe");
dict.add("some");
}
public static DefaultTreeModel treemodel = new DefaultTreeModel(null);
private boolean searchNode(String input, DefaultMutableTreeNode parent) {
if(null == parent || null == input)
return false;
DefaultMutableTreeNode child;
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
child=(DefaultMutableTreeNode)parent.getChildAt(i);
if(input.equals(child.getUserObject()) || searchNode(input, child))
return true;
}
return false;
}
private DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String input) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode(input);
if(null == parent) {
treemodel.setRoot(child);
} else {
parent.add(child);
}
return child;
}
public void printTree(DefaultMutableTreeNode node) {
int childCount = node.getChildCount();
if(0==childCount) {
String path[]=new String[node.getUserObjectPath().length];
System.arraycopy(node.getUserObjectPath(), 0, path, 0, node.getUserObjectPath().length);
for (String ptr : path)
System.out.print(ptr + " ");
System.out.println();
return;
}
for(int i=0; i<childCount; i++) {
printTree((DefaultMutableTreeNode)node.getChildAt(i));
}
}
public void SegmentString(String input, DefaultMutableTreeNode parent) {
if (searchNode(input, (DefaultMutableTreeNode)treemodel.getRoot())) {
addNode(parent, input);
return;
}
int len = input.length();
for (int i = 1; i <= len; i++) {
String prefix = input.substring(0, i);
if (dict.contains(prefix)) {
DefaultMutableTreeNode newChild = addNode(parent, prefix);
String suffix = input.substring(i, len);
SegmentString(suffix, newChild);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SegmentWord segmentWord = new SegmentWord();
segmentWord.SegmentString("thisisawesome", null);
segmentWord.printTree((DefaultMutableTreeNode)treemodel.getRoot());
}
}
I feel this gives only one answer
- rajeevkrishnanv November 19, 2012Written in Java:
Complexity : O(number of digits)
package com.chinese;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NoFour {
/**
* @param args
*/
private int inputNumber = 0;
private boolean isInputNumValid = false;
public boolean isInputNumValid() {
return isInputNumValid;
}
public NoFour(String sInputNum) {
validateInputNum(sInputNum);
}
private void validateInputNum(String sInputNum) {
try {
if (-1 == sInputNum.indexOf("4")) {
inputNumber = Integer.parseInt(sInputNum);
isInputNumValid = true;
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
System.err.println("Not a good input");
}
}
public int generateChineseNum() {
int result = inputNumber, pow = 0;
// int noOfNumberWith4=0;
int temp;
while (inputNumber > 0) {
temp = inputNumber % 10;
if (temp > 4)
result--;
result -= (temp * pow++);
inputNumber /= 10;
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println("Input the number? ");
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String num = reader.readLine();
NoFour noFour = new NoFour(num);
if (noFour.isInputNumValid())
System.out.println("Number= " + noFour.generateChineseNum());
else
System.err.println("Invalid Input");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- rajeevkrishnanv October 25, 2013