~800ms Ruby solution


  • 0
    A
    def max_product(words)
            bit_map = Hash[words.sort!{|w1, w2| w1.length <=> w2.length}.map {|w| [w.chars.uniq.reduce(0) {|acc, c| acc |= (1 << (c.ord - 'a'.ord))}, w.length]}]
            bit_map.keys.flat_map { |k1|
                bit_map.keys.map { |k2|
                    k1 & k2 == 0 ? bit_map[k1] * bit_map[k2] : 0
                }
            }.max || 0
    end

  • 0
    S

    This solution is certainly clean and easy to follow, but it really takes advantage of Ruby's beautiful syntax, which hides a lot of complexity and inefficiency behind the scenes. For example, chars.uniq is not very performant and adds space complexity. All-in-all, though...great freekin job! Clearest answer I've come across yet. My solution worked, but failed the ridiculous input at the end which resulted in an overall failure.


Log in to reply
 

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