|
| 1 | +''' |
| 2 | +A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it equals the query. (We may insert each character at any position, and may insert 0 characters.) |
| 3 | +
|
| 4 | +Given a list of queries, and a pattern, return an answer list of booleans, where answer[i] is true if and only if queries[i] matches the pattern. |
| 5 | +
|
| 6 | + |
| 7 | +
|
| 8 | +Example 1: |
| 9 | +
|
| 10 | +Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB" |
| 11 | +Output: [true,false,true,true,false] |
| 12 | +Explanation: |
| 13 | +"FooBar" can be generated like this "F" + "oo" + "B" + "ar". |
| 14 | +"FootBall" can be generated like this "F" + "oot" + "B" + "all". |
| 15 | +"FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer". |
| 16 | +Example 2: |
| 17 | +
|
| 18 | +Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa" |
| 19 | +Output: [true,false,true,false,false] |
| 20 | +Explanation: |
| 21 | +"FooBar" can be generated like this "Fo" + "o" + "Ba" + "r". |
| 22 | +"FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll". |
| 23 | +Example 3: |
| 24 | +
|
| 25 | +Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT" |
| 26 | +Output: [false,true,false,false,false] |
| 27 | +Explanation: |
| 28 | +"FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est". |
| 29 | + |
| 30 | +
|
| 31 | +Note: |
| 32 | +
|
| 33 | +1. 1 <= queries.length <= 100 |
| 34 | +2. 1 <= queries[i].length <= 100 |
| 35 | +3. 1 <= pattern.length <= 100 |
| 36 | +4. All strings consists only of lower and upper case English letters. |
| 37 | +''' |
| 38 | + |
| 39 | +class Solution(object): |
| 40 | + def camelMatch(self, queries, pattern): |
| 41 | + """ |
| 42 | + :type queries: List[str] |
| 43 | + :type pattern: str |
| 44 | + :rtype: List[bool] |
| 45 | + """ |
| 46 | + import re |
| 47 | + result = [] |
| 48 | + patterns = re.findall('[A-Z][a-z]*', pattern) |
| 49 | + |
| 50 | + for query in queries: |
| 51 | + splitter = re.findall('[A-Z][a-z]*', query) |
| 52 | + flag = True |
| 53 | + if len(patterns) == len(splitter): |
| 54 | + for index in range(len(patterns)): |
| 55 | + # print patterns[index], splitter[index] |
| 56 | + p_i, s_i = 1, 1 |
| 57 | + if patterns[index][0] == splitter[index][0]: |
| 58 | + while p_i < len(patterns[index]) and s_i < len(splitter[index]): |
| 59 | + if patterns[index][p_i] == splitter[index][s_i]: |
| 60 | + p_i += 1 |
| 61 | + s_i += 1 |
| 62 | + else: |
| 63 | + s_i += 1 |
| 64 | + if p_i != len(patterns[index]): |
| 65 | + flag = False |
| 66 | + break |
| 67 | + else: |
| 68 | + flag = False |
| 69 | + break |
| 70 | + if flag: |
| 71 | + result.append(True) |
| 72 | + else: |
| 73 | + result.append(False) |
| 74 | + else: |
| 75 | + result.append(False) |
| 76 | + return result |
0 commit comments