首页 > 其他 > 详细

44 Wildcard Matching

时间:2018-08-10 15:04:42      阅读:134      评论:0      收藏:0      [点我收藏+]
44 Wildcard Matching


https://www.youtube.com/watch?v=3ZDZ-N0EPV0&t=423s


intialization: 
Empty string and empty pattern : true 
Empty string and “*” = true
Others false , since the initialized default value for the Boolean 2d array are false, so we 
Only need to change the true part 

Same as after two cases, else, we should return false, 
And since the default initialized value are false, and 
If the two cases didn’t turn it into true, and it should be 
Default value as false






Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5






class Solution {
    public boolean isMatch(String s, String p) {
     
      
      
      // replace multiple * with one * 
      // e.g. a**b***c --> a*b*c
      boolean met = false;
      StringBuilder sb = new StringBuilder();
      for(int i = 0; i < p.length(); i++){
        if(p.charAt(i) != ‘*‘){
          sb.append(p.charAt(i));
          met = false;
        }
        if(p.charAt(i) == ‘*‘ && met == false){
          sb.append(‘*‘);
          met = true;
        }else{
          continue;
        }
      }
      
      
      char[] str = s.toCharArray();
      char[] pattern = sb.toString().toCharArray();
      
      
      boolean T[][] = new boolean[str.length + 1][pattern.length + 1];
      
      if(pattern.length > 0 && pattern[0] == ‘*‘){
        T[0][1] = true;
      }
      
      T[0][0] = true;
      
      for(int i = 1; i < T.length; i++){
        for(int j = 1; j < T[0].length; j++){
          // why it‘s j-1, because the pattern starts from 0 index
          if(pattern[j-1] == str[i-1] || pattern[j-1] == ‘?‘){
            T[i][j] = T[i-1][j-1];
          }else if (pattern[j-1] == ‘*‘){
            T[i][j] = T[i-1][j] || T[j-1][i];
          }
        }
      }
      return T[str.length][pattern.length];
    }
}

 

44 Wildcard Matching

原文:https://www.cnblogs.com/tobeabetterpig/p/9454956.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!