[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 <= queries.length <= 100
1 <= queries[i].length <= 100
1 <= pattern.length <= 100
- 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 <= 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 }
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 }
转载于:https://www.cnblogs.com/strengthen/p/10668058.html
[Swift]LeetCode1023. 驼峰式匹配 | Camelcase Matching相关推荐
- LeetCode 1023. 驼峰式匹配(暴力匹配)
1. 题目 如果我们可以将小写字母插入模式串 pattern 得到待查询项 query,那么待查询项与给定模式串匹配.(我们可以在任何位置插入每个字符,也可以插入 0 个字符.) 给定待查询列表 qu ...
- IDEA驼峰式命名插件CamelCase
我们平时在开发的时候写mybatis的sql的时候,一般都要求写驼峰式命名后传参,但是数据库里面的字段都是下划线的,有些表字非常多,一个一个来手动驼峰很不愉快,这个时候我们的插件就来了,让你快速一键搞 ...
- 驼峰命名法(camelCase)
骆驼式命名法(又称驼峰命名法),正如它的名称CamelCase所表示的那样,是指混合使用大小写字母来构成变量和函数的名字.程序员们为了自己的代码能更容易的在同行之间交流,所以多采取统一的可读性比较好的 ...
- 驼峰式与下划线命名规则
在实际代码开发过程中,代码编写格式清晰与否不仅决定了自己的代码编写与维护成本,也直接影响到项目的开发进度.编码中常用的有驼峰法和下划线两种编码格式,其中驼峰法常用在面向对象的高层语言中,下划线方法常用 ...
- PHP将带有下划线多元数组键值转为驼峰式
/** * 将下划线命名转换为驼峰式命名 * * @param $str * @param bool $ucfirst * * @return string|string[] */ function ...
- java中驼峰编码,驼峰式命名法_小驼峰式命名法编程_java中getter和setter
人们交流靠各种语言,每行都有每行的所谓的"行话".程序员也不例外,众所周知,程序员都是用代码进行交流的.那么除了在代码中的注释之外, 程序员如何读懂别人的程序呢? 当然,程序员之间 ...
- php 下划线转大写开头,使用PHP把下划线分隔命名的字符串 转换成驼峰式命名方式 , 把下划线后面的第一个字母变成大写...
最近项目使用symfony框架,这个框架对数据库的操作在这个团队里使用的是ORM进行操作,说实话使用ORM的开发效率和运行效率不一定高多少,到是它的实体命名和现有数据库字段的命名不太一样,ORM实体属 ...
- springboot mybatis plus 关闭驼峰式命名转换为下划线
springboot mybatis plus 关闭驼峰式命名转换为下划线 报错信息是这样的: org.springframework.jdbc.BadSqlGrammarException: ### ...
- PHP把下划线分隔命名的字符串 转换成驼峰式命名方式
<?php //微秒时间 function microtime_float() {list($usec, $sec) = explode(" ", microtime()); ...
最新文章
- mysql 生产实践_mysql-主从复制
- 互联网公司“黑话”大全,各个岗位都躺枪了!
- 第五章 APP元素定位
- 2022 SpringBoot的房屋租赁平台 房屋展示平台 留学生房屋租赁平台
- CentOS yum 一次性安装所需要的依赖库。
- 【代码审计】代码安全测试的方法
- JavaScript 原生Ajax
- 访问控制列表——ACL
- python复制网页文字_我用Python在网上复制文字的几种实用方法
- _ASSERTE(_CrtIsValidHeapPointer(block))
- 张量的概念及基本运算
- 无页面刷新 文件上传
- HBuilderX、微信开发者工具、VScode之间运行微信公众号
- 写给你看的Python Web 岗位分析,求职必备
- 淘宝天猫店招空白间隔去除
- 微信小程序,不可不知的一二三四
- Windows11中文原版镜像系统ISO下载
- javascript Date属性(月份英语)
- 下机数据处理:拼接、过滤和去嵌合
- 冈萨雷斯《数字图像处理》学习笔记(六)彩色图像处理
热门文章
- opencv viz3d 中的坐标系
- Farthest sampling on 3d mesh with mesh kept
- 任意输入三个英文字母,按照字典顺序输出
- 【java】switch的用法介绍
- 用python开启相机_使用“打开”编辑相机设置
- 学生HTML5今后的打算,今后我打算小学生日记
- mysql分组和where条件查询,mysql中where和having条件查询的区别
- d - 数据结构实验之查找四:二分查找_数据结构与算法笔记
- Ruby在windows下配置所遇到的问题
- c语言 0x12ed,C语言基本数据类型及运算题库有答案.doc