44 Wildcard Matching

Posted tobeabetterpig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了44 Wildcard Matching相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

44. Wildcard Matching

44. Wildcard Matching(js)

第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

leetcode 44. Wildcard Matching

Leetcode 44: Wildcard Matching

Leetcode 44: Wildcard Matching