【LEETCODE】70、字符匹配1023 Camelcase Matching
最近做leetcode总感觉自己是个智障,基本很少有题能自己独立做出来,都是百度。。。
不过终于还是做出了一题。。。而且速度效率还可以
哎,加油吧,尽量锤炼自己
package y2019.Algorithm.str.medium; import java.util.ArrayList;
import java.util.List; /**
* @Auther: xiaof
* @Date: 2019/11/21 09:00
* @Description: 1023. Camelcase Matching
*
* 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".
*
*/
public class CamelMatch { /**
* myself
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Camelcase Matching.
* Memory Usage: 34.7 MB, less than 100.00% of Java online submissions for Camelcase Matching.
* @param queries
* @param pattern
* @return
*/
public List<Boolean> solution(String[] queries, String pattern) { List<Boolean> res = new ArrayList<>();
//比较所有的字符
//1.长度pattern肯定是不比queriers长的
for (int i = 0; i < queries.length; ++i) {
res.add(match(queries[i], pattern));
} return res;
} public boolean match(String des, String pattern) {
//1.顺序比较字符,当所有的字符被des匹配成功,那么就ok,并且不能存在大写的字符留存
int index1 = 0, index2 = 0;
while (index1 < des.length() && index2 < pattern.length()) {
char desc1 = des.charAt(index1);
char p1 = pattern.charAt(index2);
//如果匹配成功,那么直接进入下一个字符
if (desc1 == p1) {
index1++;
index2++;
} else {
//如果第一个匹配失败
if (desc1 - 'a' >= 0 && desc1 - 'z' <= 0) {
//如果是小写
//2.如果是小写,那么进入下一个字符
index1++;
} else {
//1.判断字符是否小写,如果是大写
//如果大写字符不匹配,那么就直接false
return false;
}
}
}
//如果判断剩下的是否有大写
while (index1 < des.length()) {
char desc1 = des.charAt(index1);
if (desc1 - 'a' >= 0 && desc1 - 'z' <= 0) {
//如果是小写
//2.如果是小写,那么进入下一个字符
index1++;
} else {
return false;
}
} return index2 >= pattern.length();
} // ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"]
// "FB"
public static void main(String[] args) {
String[] s = {"FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"};
String pattern = "FB";
CamelMatch fuc = new CamelMatch(); fuc.solution(s, pattern); } }
【LEETCODE】70、字符匹配1023 Camelcase Matching的更多相关文章
- leetcode 44 字符匹配
题意:s是空串或包含a-z字母: p为包含a-z字母或?或 * (其中*可以匹配任意字符串包括空串,?可以匹配任意字符). 思路: 1)特殊情况:当s为空串时,p为连续 * 时,则连续 * 的位置都为 ...
- 【LeetCode】1023. Camelcase Matching 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+字典 日期 题目地址:https://leet ...
- 1023. Camelcase Matching驼峰式匹配
网址:https://leetcode.com/problems/camelcase-matching/ 依题意可得逻辑 class Solution { public: vector<bool ...
- Leetcode 1023. Camelcase Matching
暴力查找 class Solution: def camelMatch(self, queries: List[str], pattern: str) -> List[bool]: q_size ...
- 【leetcode】1023. Camelcase Matching
题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...
- LeetCode 44. 通配符匹配(Wildcard Matching)
题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...
- Leetcode 10. 正则表达式匹配 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Java实现 LeetCode 10 正则表达式匹配
10. 正则表达式匹配 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符 '*' 匹配零个或多个前面的那一个元素 所谓匹配, ...
- 【编程题目】有 n 个长为 m+1 的字符串,如果某个字符串的最后 m 个字符与某个字符串的前 m 个字符匹配...
37.(字符串)有 n 个长为 m+1 的字符串,如果某个字符串的最后 m 个字符与某个字符串的前 m 个字符匹配,则两个字符串可以联接,问这 n 个字符串最多可以连成一个多长的字符串,如果出现循环, ...
随机推荐
- luoguP3964 [TJOI2013]松鼠聚会
链接 luogu 思路 切比雪夫距离有max,不好优化. 但是我们能转化成曼哈顿距离,只需要 \((x,y)变成(\frac{x+y}{2},\frac{x-y}{2})\) 相反的曼哈顿距离转切比雪 ...
- 洛谷 P1079 Vigenère 密码
目录 题目 思路 \(Code\) 题目 P1079 Vigenère 密码 思路 字符串+模拟.仔细读题,然后仔细敲代码(说了和没说一样)... \(Code\) #include<iostr ...
- 复旦高等代数I(19级)每周一题
本学期的高等代数每周一题活动计划从第2教学周开始,到第15教学周结束,每周的周末公布一道思考题(共14道,思考题一般与下周授课内容密切相关),供大家思考和解答.每周一题将通过“高等代数官方博客”(以博 ...
- nginx 反向代理之 负载均衡
Nginx通过upstream和proxy_pass实现了负载均衡.本质上也是Nginx的反向代理功能,只不过后端的server为多个. 案例一(简单的轮询): upstream www { serv ...
- Spring通知,顾问,增强
1.AOP (Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编 ...
- Spring Boot进阶系列一
笔者最近在总结一个 Spring Boot实战系列,以方便将来查找和公司内部培训用途. 1.Springboot从哪里来 SpringBoot是由Pivotal团队在2013年开始研发.2014年4月 ...
- 数据结构(一)二叉树 & avl树 & 红黑树 & B-树 & B+树 & B*树 & R树
参考文档: avl树:http://lib.csdn.net/article/datastructure/9204 avl树:http://blog.csdn.net/javazejian/artic ...
- java集合代码示例
一.List ArrayList 使用List时,最好初始化容量. ArrayList的默认容量为10,每次扩容增0.5倍,假如要放置100个元素,需要多次扩容. List<String> ...
- JS简单获取当前日期时间的方法(yyyy-MM-dd hh:mm:ss)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 深入分析Synchronized原理(阿里面试题)
还有一篇 讲解lock的实现原理,参考:解决多线程安全问题-无非两个方法synchronized和lock 具体原理以及如何 获取锁AQS算法 (百度-美团) 记得开始学习Java的时候,一遇到多线程 ...