JavaScript solution using .replace, .find and .startsWith


  • 0
    D
    function replaceWords(dict, sentence) {
      return sentence.replace(/\b\w+\b/g, (word) => {
        return dict.find(root => word.startsWith(root)) || word; 
      });
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.