I got the following question in the coding interview at Google.
Given a Tree Node representation for a regular expression, we consider the following cases:
// 'CHAR' a : matches only a, and not aa
// 'DISJ' a|b : matches a or b
// 'REP' a*: matches empty string or a or aa or aaa...
// 'CONCAT' a(b) : matches ab
Example: a(a|b)* valid examples: a, ab, aa, aab, aba, aaaaa, abbbb, ...
Represented in this tree:
CONCAT
/ \
CHAR (a) REP
/
DISJ
/ \
CHAR a CHAR bProvided the followng Class Node
enum Type {
CHAR,
DISJ,
REP,
CONCAT
}
static class Node {
Type type;
Node left;
Node right;
char c;
}Impleent the Match method
public static boolean isMatch(String s, Node n) {
}Edit: Moving my implementation to the comments to avoid confusing readers about the solution. https://leetcode.com/discuss/interview-question/1946698/Google-or-Onsite-or-Implement-match-method-for-Regular-Expression-Tree/1351804
I implemented the solution by traversing the tree as I mentioned in the comments and walked through the example and it seems to work.
However the interviewer said it doesn't work and said the method should take start and end instead of indx.