JSFiddle - React, Tailwind, and code Playground

by jayakrishnancn

JavaScript

class Node{
    data = null;
    isEnd = false;
    isLoop = false;
    next = null;
    constructor(char = null){
        this.data = char;
    }


}
function isCharMatch(p,c){
    return p ==='.' || c === p
}

function isMatch(s: string, p: string): boolean {
    let root = new Node();
    let node = root;
    
    for(let char of p){
        if(char === '*'){
            node.isLoop = true;
            continue;
        }
        node.next = new Node(char)
        node = node.next
    }    
    node.isEnd = true;   
    
    node = root.next;
    for(let char of s){ 
    
        if(!node){
            return false;
        }
        
        if(node.data.isLoop && !isCharMatch(node.data, char)){
            node = node.next;
        }
        
        if(node.data !== '.' && !isCharMatch(node.data,char)){
            return false
        } 
        
        if(!node.isLoop){
            node = node.next;            
        }
        
    }
    return node.isEnd;
    
    
    
    
};