AC solution for ruby


  • 0
    A

    could this be improved ??

    def is_isomorphic(s, t)
      if s.length != t.length
        return false
      end
        
      existed_char_a = {}
      existed_char_b = {}
    
      (0..s.length - 1).each do |i|
        if existed_char_a.has_key?(s[i]) || existed_char_b.has_key?(t[i])
          if existed_char_a[s[i]] != t[i] || existed_char_b[t[i]] != s[i]
            return false
          end
        else
          existed_char_a[s[i]] = t[i]
          existed_char_b[t[i]] = s[i]
        end
      end
        
      return true
    end

Log in to reply
 

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