Created
May 4, 2016 04:52
-
-
Save temirov/a72f7244c055a1d237034cb40f629c5c to your computer and use it in GitHub Desktop.
Test string isomorphism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CoreExtensions | |
module Histogramable | |
refine String do | |
def to_histogram | |
each_char.with_index.each_with_object(Hash.new { |hsh, key| hsh[key] = [] }) { |(ch, index), o| o[ch] << index } | |
end | |
end | |
end | |
end | |
class Isomorphism | |
using CoreExtensions::Histogramable | |
attr_accessor :str1, :str2 | |
def initialize(str1:, str2:) | |
@str1, @str2 = str1, str2 | |
end | |
def isomorphic? | |
(@str1.to_histogram.values - @str2.to_histogram.values).count.zero? | |
end | |
end | |
test1 = Isomorphism.new(str1: 'foo', str2: 'bar') | |
test2 = Isomorphism.new(str1: 'egg', str2: 'odd') | |
test1.isomorphic? | |
test2.isomorphic? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment