最近作leetcode總感受本身是個智障,基本不多有題能本身獨立作出來,都是百度。。。java
不過終於仍是作出了一題。。。並且速度效率還能夠less
哎,加油吧,儘可能錘鍊本身this
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); } }