LEETCODE70字符匹配1023 Camelcase Matching

Posted cutter_point

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LEETCODE70字符匹配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);

    }

}

 

以上是关于LEETCODE70字符匹配1023 Camelcase Matching的主要内容,如果未能解决你的问题,请参考以下文章

算法千题案例每日LeetCode打卡——70.字符串中的第一个唯一字符

算法千题案例每日LeetCode打卡——70.字符串中的第一个唯一字符

leetcode70word-search

LeetCode 44 通配符匹配

LeetCode 1408 数组中的字符串匹配[字符串 模式匹配] HERODING的LeetCode之路

LeetCode 1408. 数组中的字符串匹配