zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1023. 驼峰式匹配 | Camelcase Matching

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/10668058.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.)

    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.

    Example 1:

    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:

    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:

    1. 1 <= queries.length <= 100
    2. 1 <= queries[i].length <= 100
    3. 1 <= pattern.length <= 100
    4. All strings consists only of lower and upper case English letters.

    如果我们可以将小写字母插入模式串 pattern 得到待查询项 query,那么待查询项与给定模式串匹配。(我们可以在任何位置插入每个字符,也可以插入 0 个字符。)

    给定待查询列表 queries,和模式串 pattern,返回由布尔值组成的答案列表 answer。只有在待查项 queries[i] 与模式串 pattern匹配时, answer[i] 才为 true,否则为 false

    示例 1:

    输入: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. 1 <= queries.length <= 100
    2. 1 <= queries[i].length <= 100
    3. 1 <= pattern.length <= 100
    4. 所有字符串都仅由大写和小写英文字母组成。

    Runtime: 8 ms
    Memory Usage: 19.9 MB
     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 }

    8ms
     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 }
  • 相关阅读:
    centos7下安装docker(2镜像)
    Centos7下安装docker(1)
    分享一个连接,升级内核
    zabbix图形乱码问题解决办法
    六十四:JavaScript之JavaScript的Math对象常用的方法
    六十三:JavaScript之JavaScript的String对象常用的方法
    六十二:JavaScript之JavaScript数组对象和常用的方法
    六十一:JavaScript之JavaScript函数
    六十:JavaScript之JavaScript流程控制语句
    五十九:JavaScript之JavaScript操作符
  • 原文地址:https://www.cnblogs.com/strengthen/p/10668058.html
Copyright © 2011-2022 走看看