【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);

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章