public boolean goThrough(TN head, int i) { if (i == ls.length) { if (head.get('#') != null) return true; else return false; } TN p1 = head.get(ls[i]); TN p2 = head.get('#'); boolean b1 = false; boolean b2 = false; if (p1 != null) b1 = goThrough(p1, i + 1); if (b1) return true; if (p2 != null) b2 = goThrough(p2, i); if (b2) return true; return false; }
public TN buildTire(Set<String> dict) { TN head = new TN('#'); TN fatherNode = head; for (String s : dict) { fatherNode = head; char[] ss = s.toCharArray(); if (ss.length == 0) continue; for (int i = 0; i < ss.length; i++) { TN tnode = fatherNode.get(ss[i]); if (tnode == null) tnode = fatherNode.add(ss[i]); fatherNode = tnode; } fatherNode.ends(head); } return head; }