Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var isMatch = function(text, pattern, mem=new Map()) {
- let key = text + '__' + pattern, pLen = pattern.length,
- tLen = text.length, first_match;
- if (!mem.has(key)) {
- if (pLen === 0) {
- mem.set(key, tLen === 0);
- return tLen === 0;
- }
- first_match = (tLen !== 0 && (pattern[0] == text[0] ||
- pattern[0] == '.'));
- if (pLen >= 2 && pattern[1] == '*'){
- mem.set(key, (isMatch(text, pattern.substring(2), mem) ||
- (first_match && isMatch(text.substring(1), pattern, mem))));
- } else {
- mem.set(key, first_match && isMatch(text.substring(1),
- pattern.substring(1), mem));
- }
- }
- return mem.get(key);
- };
Add Comment
Please, Sign In to add comment