[LeetCode]#10 Regular Expression Matching javascript字符串包含

https://leetcode.com/problems/regular-expression-matching/

* isMatch.js 

/**
 * @param {string} s: text
 * @param {string} p: pattern
 * @return {boolean}
 */
var isMatch = function(s, p) {
    if (p.isEmpty()) {
	return s.isEmpty();
    }
    var firstMatch = false;
    if (!s.isEmpty()) {
	firstMatch = p.charAt(0) === s.charAt(0) ||
	    p.charAt(0) === ".";
    }
    if (p.length >= 2 && p.charAt(1) === "*") {
	return isMatch(s, p.substring(2)) ||
	    (firstMatch && isMatch(s.substring(1), p))
    }
    return firstMatch && isMatch(s.substring(1), p.substring(1));
};

String.prototype.isEmpty = function() {
    return this.equals("");
}

String.prototype.equals = function(s) {
    if (this.length !== s.length) {
	return false;
    }
    for (var i = 0; i < this.length; i++) {
	if (this.charCodeAt(i) !== s.charCodeAt(i)) {
	    return false;
	}
    }
    return true;
}

console.log(isMatch("aa", "a"));  // false

console.log(isMatch("aa", "a*")); // true

console.log(isMatch("abcdef", ".*"));  // true

console.log(isMatch("aab", "c*a*b"));  // true

console.log( isMatch("mississippi", "mis*is*p*.") ); // false

 

动态规划法:

/**
 * @param {string} s: text
 * @param {string} p: pattern
 * @return {boolean}
 */
var isMatch = function(s, p) {
    var memo = [];
    for (var i = 0; i <= s.length; i++) {
        memo[i] = [];
        for (var j = 0; j <= p.length; j++) {
            memo[i][j] = null;
        }
    }
    return dp(memo, 0, 0, s, p);
};

const Result = {
  FALSE: 0,
  TRUE: 1
};

function dp(memo, i, j, s, p) {
    if (memo[i][j] !== null) {
        return memo[i][j] === Result.TRUE;
    }
    var ans = false;
    if (j === p.length) {
        ans = i === s.length;
    } else {
        var firstMatch = ( i < s.length ) &&
        (p.charAt(j) === s.charAt(i) || p.charAt(j)=== ".");
        
        if (j+1 < p.length && p.charAt(j+1)==="*") {
            ans = ( dp(memo, i, j+2, s, p) ||
                  firstMatch && dp(memo, i+1, j, s, p));
        } else {
            ans = firstMatch && dp(memo, i+1, j+1, s, p);
        }
    }
    memo[i][j] = ans ? Result.TRUE : Result.FALSE;
    return ans;
}

 

String.prototype.contains = function(s) {
    var i;
    if (s.length < 2) {
	for (i = 0; i < this.length; i++) {
	    if (this.charCodeAt(i) === s.charCodeAt(0)) {
		return true;
	    }
	}
	return false;
    }
    var j = 0;
    var enter = false;
    for (i = 0; i < this.length; i++) {
	if (enter) {
	    if (j < s.length) {
		if (this.charCodeAt(i) === s.charCodeAt(j)) {
		    j++;
		} else {
		    j = 0;
		    enter = false;
		}
	    } else if (j === s.length) {
		return true;
	    } else {
		throw new Error("Can\'t reach here...");
	    }
	} else {
	    if (this.charCodeAt(i) === s.charCodeAt(j)) {
		enter = true;
		j++;
	    }
	}
    }
    return (j === s.length);
};

console.log("abcd".contains("bc")); // true
console.log("abcd".contains("bcd"));// true
console.log("abcd".contains("abcd")); // true
console.log("abcd".contains("abc")); // true
console.log("abcd".contains("ad")); // false
console.log("abcd".contains("ab"));  // true
console.log("abcd".contains("e")); // false

 

Solution.js

const Result = {
  FALSE: 0,
  TRUE: 1
};

function Solution() {}

Solution.memo = [];

/**
 * @param text String
 * @param pattern String
 * @returns Boolean
 */
Solution.isMatch = function(text, pattern) {
    Solution.memo = new Array(text.length+1);
    for (var i = 0; i < Solution.memo.length; i++) {
	Solution.memo[i] = new Array(pattern.length+1).fill(null);
    }
//    console.log(Solution.memo);
    return Solution.dp(0, 0, text, pattern);
};

Array.prototype.fill = function(value, start, end) {
    start = start || 0;
    end = end || this.length;
    for (var i = start; i < end; i++) {
	this[i] = value;
    }
    return this;
}

/**
 * Dynamic programming
 * @param i int
 * @param j int
 * @param text String
 * @param pattern String
 * @returns {} Boolean
 */
Solution.dp = function(i, j, text, pattern) {
  let memo = Solution.memo, dp = Solution.dp;

  if (memo[i][j] !== null) {
    return memo[i][j] === Result.TRUE;
  }
  var ans = false;

  if (j === pattern.length) {
    return i === text.length;
  }
    var firstMatch = ( i < text.length ) &&
      (pattern.charAt(j) === text.charAt(i) || pattern.charAt(j) === ".");

    if (j + 1 < pattern.length && pattern.charAt(j+1) === "*") {
      ans = ( dp(i, j+2, text, pattern) ||
        firstMatch && dp(i+1, j, text, pattern) );
    } else {
      ans = firstMatch && dp( i+1, j+1, text, pattern );
    }
    memo[i][j] = ans ? Result.TRUE : Result.FALSE;
    
  return ans;
};

module.exports = Solution;

* index.js

var Solution = require('./Solution');

var isMatch = Solution.isMatch;

console.log(isMatch("aa", "a"));  // false
console.log(isMatch("aa", "a*")); // true
console.log(isMatch("abcdef", ".*"));  // true
console.log(isMatch("aab", "c*a*b"));  // true
console.log( isMatch("mississippi", "mis*is*p*.") ); // false

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值