https://leetcode.com/problems/regular-expression-matching/javascript
* isMatch.js java
/** * @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")); // falseexpress
console.log(isMatch("aa", "a*")); // trueide
console.log(isMatch("abcdef", ".*")); // trueui
console.log(isMatch("aab", "c*a*b")); // truethis
console.log( isMatch("mississippi", "mis*is*p*.") ); // falseprototype
動態規劃法:code
/** * @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")); // falseip
Solution.jsleetcode
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