Pattern Overlap
InterviewMemory limit1024 MB
Given two patterns where each star matches 0 to 4 letters, decide whether some title matches both patterns.
- Level
Medium5 of 10
- Topics
- String, String matching, Dynamic programming
- Solved
- No attempts yet
Problem
Alice likes reading and buys a lot of books. She stores her books in two boxes. Each box is labeled with a pattern that matches the titles of all the books stored in that box. A pattern consists of only uppercase and lowercase English letters and stars (*). A star can match between zero and four letters. For example, books with the titles GoneGirl and GoneTomorrow can be put in a box with the pattern Gone**, but books with the titles TheGoneGirl, Gonetomorrow, and GoneWithTheWind cannot.
Alice wants to know whether any book can be stored in either box. In other words, she wants to know whether there is a title that matches both boxes' patterns.
Input
The first line contains the number of test cases, T. Then T test cases follow. Each test case consists of two lines, and each line contains one string made of uppercase and lowercase English letters and *.
Output
For each test case, output one line in the format Case #x: y. Here, x is the test case number, starting from 1. y is TRUE if some string matches both patterns, and FALSE otherwise.
Limits
1 ≤ T ≤ 50.
Hint
In the first test case, the title It matches both patterns. A star can match zero characters.
In the second test case, the title Shakespeare matches both patterns.
In the third test case, no title matches both patterns. For example, Shakespeare does not work, because the leading * in the pattern *peare cannot match six letters.