|
| 1 | +#!/usr/bin/python3 |
| 2 | +""" |
| 3 | +A query word matches a given pattern if we can insert lowercase letters to the |
| 4 | +pattern word so that it equals the query. (We may insert each character at any |
| 5 | +position, and may insert 0 characters.) |
| 6 | +
|
| 7 | +Given a list of queries, and a pattern, return an answer list of booleans, where |
| 8 | +answer[i] is true if and only if queries[i] matches the pattern. |
| 9 | +
|
| 10 | +Example 1: |
| 11 | +
|
| 12 | +Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer", |
| 13 | +"ForceFeedBack"], pattern = "FB" |
| 14 | +Output: [true,false,true,true,false] |
| 15 | +Explanation: |
| 16 | +"FooBar" can be generated like this "F" + "oo" + "B" + "ar". |
| 17 | +"FootBall" can be generated like this "F" + "oot" + "B" + "all". |
| 18 | +"FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer". |
| 19 | +Example 2: |
| 20 | +
|
| 21 | +Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer", |
| 22 | +"ForceFeedBack"], pattern = "FoBa" |
| 23 | +Output: [true,false,true,false,false] |
| 24 | +Explanation: |
| 25 | +"FooBar" can be generated like this "Fo" + "o" + "Ba" + "r". |
| 26 | +"FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll". |
| 27 | +Example 3: |
| 28 | +
|
| 29 | +Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer", |
| 30 | +"ForceFeedBack"], pattern = "FoBaT" |
| 31 | +Output: [false,true,false,false,false] |
| 32 | +Explanation: |
| 33 | +"FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est". |
| 34 | +
|
| 35 | +Note: |
| 36 | +1 <= queries.length <= 100 |
| 37 | +1 <= queries[i].length <= 100 |
| 38 | +1 <= pattern.length <= 100 |
| 39 | +All strings consists only of lower and upper case English letters. |
| 40 | +""" |
| 41 | +fromtypingimportList |
| 42 | + |
| 43 | + |
| 44 | +classSolution: |
| 45 | +defcamelMatch(self,queries:List[str],pattern:str)->List[bool]: |
| 46 | +ret= [] |
| 47 | +forqinqueries: |
| 48 | +ret.append(self.match(q,pattern)) |
| 49 | + |
| 50 | +returnret |
| 51 | + |
| 52 | +defmatch(self,q,p): |
| 53 | +i=0 |
| 54 | +j=0 |
| 55 | +whilei<len(q)andj<len(p): |
| 56 | +ifq[i]==p[j]: |
| 57 | +i+=1 |
| 58 | +j+=1 |
| 59 | +elifq[i].islower(): |
| 60 | +i+=1 |
| 61 | +else: |
| 62 | +break |
| 63 | + |
| 64 | +whilei<len(q)andq[i].islower(): |
| 65 | +i+=1 |
| 66 | + |
| 67 | +returni==len(q)andj==len(p) |
| 68 | + |
| 69 | + |
| 70 | +if__name__=="__main__": |
| 71 | +assertSolution().camelMatch(["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"],"FoBa")== [True,False,True,False,False] |