Topic describes

Their thinking

  • The question uses the idea of backtracking.
  • The main considerations are as follows
  1. The characters of a string are equal to those of a pattern string.
  2. The characters of the pattern string are dots.
  3. When the mode string is *, there are two cases to discuss, one is 0 times, the other is multiple times.

The problem solving code

var isMatch = function (s, p) {
    return helper(s, p);
    function helper(str, pattern) {
        if (pattern.length === 0) {
            if (str.length === 0) {
                return true;
            } else {
                return false; }}// This is not the case with *
        if (str.length > 2 && (str[0] === pattern[0] || pattern[0= = ='. ') && pattern[1]! = =The '*') {
            res = helper(str.slice(1),pattern.slice(1));
        } else if (str.length > 0 && (str[0] === pattern[0] || pattern[0= = ='. ') && pattern[1= = =The '*') {// This is the case where * represents 0 times
            res = helper(str,pattern.slice(2)) || helper(str.slice(1),pattern)
        } else {
            if (str.length > 0 && (str[0] === pattern[0] || pattern[0= = ='. ') && pattern[1]! = =The '*') {
                res = helper(str.slice(1),pattern.slice(1)); 
            } else if (pattern[1= = =The '*') {
                res = helper(str,pattern.slice(2))}else {
                return false; }}returnres; }};Copy the code

revelation

  • Learn to use recursive backtracking to solve problems under multiple constraints.

Reference links (see the one below first)

Juejin. Cn/post / 696319…