Ruby 3 line solution


  • 0
    R
    def first_uniq_char(s)
        return -1 if s.nil? || s.empty?
        unique_char = s.chars.each_with_object(Hash.new(0)) { |char, hash| hash[char] += 1 }.key(1)
        unique_char ? s.index(unique_char) : -1
    end
    

Log in to reply
 

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