Facebook | Design data structure for wildcard search

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2016-08-15Added By Anonymous
Source: Link

Company: Meta
Role: unknown
Interview Stage: unknown
Location: unknown
Year: unknown

Original Post Content:

Given such data structure, implement add() and search() method, which support wildcard matching.
? Matches any single character.
* Matches any sequence of characters (including the empty sequence).

class Node
{
Node getChildForLetter(char letter)
Node[] getAllChildren();
boolean isTerminal();
}

//some example:
add("car")
add("caw")
add("cauw")

search("c*w")
//should return "caw" and "cauw".
// "*" could be at any place in the input string.

This problem is pretty similar to 211. Add and Search Word - Data structure design, but you need to also implement wildcard searching. The brute-force way is not hard to find out, but what about the optimization, any thought on this?

Comments