【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 个字符串最多可以连成一个多长的字符串,如果出现循环, ...
随机推荐
- js中的逗号运算符
逗号运算符 逗号运算符是二元运算符,它的操作数可以是任意类型.它首先计算左操作数,然后计算右操作数,最后返回右操作数的值,用逗号运算符可以在一条语句中执行多个运算 作用: 1.在一条语句中从左到右执行 ...
- K8s无状态控制器原理介绍
Pod控制器: ReplicationController:早期K8s只有这一个控制器,但后来发现让这一个来完成所有任务,太复杂.因此被废弃. ReplicaSet: 它用于帮助用户创建指定数量的Po ...
- GoCN每日新闻(2019-11-05)
GoCN每日新闻(2019-11-05) GoCN每日新闻(2019-11-05) 1. Protobuf 终极教程 https://colobu.com/2019/10/03/protobuf-ul ...
- nginx 日志之 error_log
Nginx错误日志平时不用太关注,但是一旦出了问题,就需要借助错误日志来判断问题所在. 配置参数格式:error_log /path/to/log level; Nginx错误日志级别 常见的错误日志 ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- Lucene创建索引和索引的基本检索(Lucene 之 Hello World)
Author: 百知教育 gaozhy 注:演示代码所使用jar包版本为 lucene-xxx-5.2.0.jar 一.lucene索引操作 1.创建索引代码 try { // 1. 指定索引文件存 ...
- 【深入学习linux】git的使用
git的安装 官网下载地址:https://git-scm.com/downloads 安装完成后,还需要最后一步设置,在命令行输入: $ git config --global user.name ...
- tornado多进程模式不同进程写不同日志
#coding: utf- ''' Author: Time: Target: ''' import logging import logging.handlers import os import ...
- 运维笔记--Linux查找指定目录下包含某字符串的文件
待整理: 参考:http://blog.sina.com.cn/s/blog_53d496960102xg5c.html 例: find /home/logstash/ -type f | xargs ...
- 使用 atom 将 makedown 编辑并转换成 pdf
链接: https://www.cnblogs.com/fanzhidongyzby/p/6637084.html