Super Reduced String

  • + 1 comment

    Solution using Javascript using recursion and regex:

    const regex = /([a-z])\1/g;
    
    function superReducedString(s) {
        let replaced = removeAdjacents(s);
        
        while(checkForAdjacents(replaced)) {
            replaced = removeAdjacents(replaced)
        }
        
        if(replaced === "") {
            return "Empty String"
        }
        
        return replaced
    }
    
    const removeAdjacents = (str) => {
        return str.replaceAll(regex, '');
    }
    
    const checkForAdjacents = (str) => {
        
        return regex.test(str);
    }