★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-ogteqzod-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.)git
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
.github
Example 1:api
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB" Output: [true,false,true,true,false] Explanation: "FooBar" can be generated like this "F" + "oo" + "B" + "ar". "FootBall" can be generated like this "F" + "oot" + "B" + "all". "FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".
Example 2:微信
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa" Output: [true,false,true,false,false] Explanation: "FooBar" can be generated like this "Fo" + "o" + "Ba" + "r". "FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".
Example 3:app
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT" Output: [false,true,false,false,false] Explanation: "FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".
Note:this
1 <= queries.length <= 100
1 <= queries[i].length <= 100
1 <= pattern.length <= 100
若是咱們能夠將小寫字母插入模式串 pattern
獲得待查詢項 query
,那麼待查詢項與給定模式串匹配。(咱們能夠在任何位置插入每一個字符,也能夠插入 0 個字符。)spa
給定待查詢列表 queries
,和模式串 pattern
,返回由布爾值組成的答案列表 answer
。只有在待查項 queries[i]
與模式串 pattern
匹配時, answer[i]
才爲 true
,不然爲 false
。code
示例 1:htm
輸入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB" 輸出:[true,false,true,true,false] 示例: "FooBar" 能夠這樣生成:"F" + "oo" + "B" + "ar"。 "FootBall" 能夠這樣生成:"F" + "oot" + "B" + "all". "FrameBuffer" 能夠這樣生成:"F" + "rame" + "B" + "uffer".
示例 2:
輸入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa" 輸出:[true,false,true,false,false] 解釋: "FooBar" 能夠這樣生成:"Fo" + "o" + "Ba" + "r". "FootBall" 能夠這樣生成:"Fo" + "ot" + "Ba" + "ll".
示例 3:
輸出:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT" 輸入:[false,true,false,false,false] 解釋: "FooBarTest" 能夠這樣生成:"Fo" + "o" + "Ba" + "r" + "T" + "est".
提示:
1 <= queries.length <= 100
1 <= queries[i].length <= 100
1 <= pattern.length <= 100
1 class Solution { 2 func camelMatch(_ queries: [String], _ pattern: String) -> [Bool] { 3 var ans:[Bool] = [Bool]() 4 for q in queries 5 { 6 ans.append(go(q,pattern)) 7 } 8 return ans 9 } 10 11 func go(_ q:String,_ p:String) -> Bool 12 { 13 var pos:Int = 0 14 var arrP:[Character] = Array(p) 15 for c in q 16 { 17 if pos < p.count && c == arrP[pos] 18 { 19 pos += 1 20 } 21 else if c < "a" || c > "z" 22 { 23 return false 24 } 25 } 26 return pos == p.count 27 } 28 }
1 import Foundation 2 3 class Solution { 4 func camelMatch(_ queries: [String], _ pattern: String) -> [Bool] { 5 var result = [Bool]() 6 let pattern = Array(pattern) 7 for q in queries { 8 result.append(check(Array(q), pattern)) 9 } 10 return result 11 } 12 13 func check(_ q: [Character], _ p: [Character]) -> Bool { 14 if p.count > q.count { return false } 15 var pIndex = 0 16 for i in 0..<q.count { 17 if pIndex > p.count - 1 { 18 if isUpperCase(q[i]) { return false } 19 } else { 20 if q[i] == p[pIndex] { 21 pIndex += 1 22 } else { 23 if isUpperCase(q[i]) { return false } 24 } 25 } 26 } 27 return pIndex == p.count 28 } 29 30 func isUpperCase(_ c: Character) -> Bool { 31 let tmd = String(c).unicodeScalars.first! 32 return CharacterSet.uppercaseLetters.contains(tmd) 33 } 34 }
12ms
1 class Solution { 2 func camelMatch(_ queries: [String], _ pattern: String) -> [Bool] { 3 var result = [Bool]() 4 for query in queries { 5 result.append(isSubsequnce(pattern, query)) 6 } 7 return result 8 } 9 10 fileprivate func isSubsequnce(_ pattern: String, _ word: String) -> Bool { 11 guard pattern.count <= word.count else { 12 return false 13 } 14 var index1 = 0, index2 = 0 15 let chars1 = Array(pattern) 16 let chars2 = Array(word) 17 18 let capitals1 = chars1.filter { String($0).uppercased() == String($0) } 19 let capitals2 = chars2.filter { String($0).uppercased() == String($0) } 20 guard capitals1 == capitals2 else { 21 return false 22 } 23 24 while index1 < chars1.count && index2 < chars2.count { 25 if chars1[index1] == chars2[index2] { 26 index1 += 1 27 index2 += 1 28 } else { 29 index2 += 1 30 } 31 } 32 return (index1 == chars1.count && index2 == chars2.count) || index1 == chars1.count 33 } 34 }