ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实现字通配符

2020-06-24 22:06:21  阅读:187  来源: 互联网

标签:charAt idx 实现 通配符 matchStr length matchIdx str


 

 

 

 

 

 

 1 import java.util.*;
 2    
 3 public class Main {
 4     private static boolean match(String matchStr, String str, int matchIdx, int idx) {
 5         if (matchIdx == matchStr.length() && idx == str.length()) {
 6             return true;
 7         }
 8         //str匹配到最后一个字符了,matchIdx后面还有多个*的情况
 9         if (idx == str.length() && matchIdx < matchStr.length() && matchStr.charAt(matchIdx) == '*') {
10            return match(matchStr, str, matchIdx + 1, idx);
11         }
12         if (matchIdx >= matchStr.length() || idx >= str.length()) {
13             return false;
14         }
15         //匹配中出现了不同的字符
16         if (matchStr.charAt(matchIdx) != '*' && matchStr.charAt(matchIdx) != str.charAt(idx)) {
17             return false;
18         }
19         boolean flag = false;
20         //匹配中对*处理,*能表示多个字符,所以idx + 1,直到到达str的最后一个字符
21         if (matchStr.charAt(matchIdx) == '*') {
22             flag = match(matchStr, str, matchIdx + 1, idx) || match(matchStr, str, matchIdx, idx + 1);
23         }
24         //匹配中两个字符串的字符相同的情况,用与或保证可以从false变回true
25         if (matchStr.charAt(matchIdx) == str.charAt(idx)) {
26             flag |= match(matchStr, str, matchIdx + 1, idx + 1);
27         }
28         return flag;
29     }
30    
31     private static List<Integer[]> getMatchPosAndLen(String matchStr, String str) {
32         List<Integer[]> ans = new ArrayList<>();
33          //找到头尾都相等的情况
34         for (int i = 0; i < str.length(); ++i) {
35             //保证第一个字符相同
36             if (matchStr.charAt(0) != '*' && matchStr.charAt(0) != str.charAt(i)) {
37                 continue;
38             }
39             
40             for (int j = i; j < str.length(); ++j) {
41                 //保证最后一个字符相同
42                 if (matchStr.charAt(matchStr.length() - 1) != '*' && matchStr.charAt(matchStr.length() - 1) != str.charAt(j)) {
43                     continue;
44                 }
45                 if (match(matchStr, str.substring(i, j + 1), 0, 0)) {
46                     ans.add(new Integer[]{i, j - i + 1});
47                 }
48             }
49         }
50         if (ans.size() == 0) {
51             ans.add(new Integer[]{-1, 0});
52         }
53         return ans;
54     }
55    
56     public static void main(String[] args) {
57         Scanner in = new Scanner(System.in);
58          String matchStr = in.next();
59         String str = in.next();
60         List<Integer[]> list = getMatchPosAndLen(matchStr, str);
61         for (Integer[] arr : list) {
62             System.out.println(arr[0] + " " + arr[1]);
63         }
64    
65     }
66 }

 

标签:charAt,idx,实现,通配符,matchStr,length,matchIdx,str
来源: https://www.cnblogs.com/gy7777777/p/13190154.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有