Two-liner Ruby Solution


  • 0
    R
    def replace_words(dict, sentence)
        dict = dict.sort_by(&:length)
        sentence.split.map { |word| dict.find{ |root| word.start_with?(root) } || word }.join(' ')  
    end
    

Log in to reply
 

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