Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. var isMatch = function(text, pattern, mem=new Map()) {
  2. let key = text + '__' + pattern, pLen = pattern.length,
  3. tLen = text.length, first_match;
  4. if (!mem.has(key)) {
  5. if (pLen === 0) {
  6. mem.set(key, tLen === 0);
  7. return tLen === 0;
  8. }
  9. first_match = (tLen !== 0 && (pattern[0] == text[0] ||
  10. pattern[0] == '.'));
  11. if (pLen >= 2 && pattern[1] == '*'){
  12. mem.set(key, (isMatch(text, pattern.substring(2), mem) ||
  13. (first_match && isMatch(text.substring(1), pattern, mem))));
  14. } else {
  15. mem.set(key, first_match && isMatch(text.substring(1),
  16. pattern.substring(1), mem));
  17. }
  18. }
  19. return mem.get(key);
  20. };
Add Comment
Please, Sign In to add comment