abettikk
BAN USERHi, just wanted to confirm one thing...
You are converting each row of the matrix into a adjacency entry ?
So for e.g. the first row "a b e d", you use "a" as the starting vertex and then "b","e","d" are treated adjacent to "a" and so on ?
I think this on its own could be a nice theoretical CS question i.e. convert an Adj. Matrix into Adj. Lists
How about this ?
public class CheckValidity {
int counter;
boolean isValid(CharStream in) {
if (in.hasNext()) {
char currChar = in.next();
if (currChar == '(' || currChar == '[') {
++counter;
} else if (currChar == ')' || currChar == ']') {
--counter;
}
} else {
return (counter == 0);
}
return isValid(in) || (counter == 0);
}
public static void main(String[] args) {
System.out.println(new CheckValidity().isValid(new CupCharStream("(xyz)".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("(xyz".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("xyz)".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("((x)y(z))".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("([[xyz])".toCharArray())));
}
}
public class CheckValidity {
int counter;
boolean isValid(CharStream in) {
if (in.hasNext()) {
char currChar = in.next();
if (currChar == '(' || currChar == '[') {
++counter;
} else if (currChar == ')' || currChar == ']') {
--counter;
}
} else {
return (counter == 0);
}
return isValid(in) || (counter == 0);
}
public static void main(String[] args) {
System.out.println(new CheckValidity().isValid(new CupCharStream("(xyz)".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("(xyz".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("xyz)".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("((x)y(z))".toCharArray())));
System.out.println(new CheckValidity().isValid(new CupCharStream("([[xyz])".toCharArray())));
}
}
When you say "from the words, figure out which letter precedes which", what exactly do you mean ?
- abettikk January 05, 2015How do we build a DAG from pre-sorted list of words and then how to chose a root against which to get a topological sort done ?