Skip to content

Instantly share code, notes, and snippets.

@jubishop
Created July 11, 2019 22:43
Show Gist options
  • Save jubishop/880043a085d22c41380a80a083976841 to your computer and use it in GitHub Desktop.
Save jubishop/880043a085d22c41380a80a083976841 to your computer and use it in GitHub Desktop.
def _is_interleave(s1, s2, s3)
return s2 == s3 if (s1.empty?)
return s1 == s3 if (s2.empty?)
return false if ($dp1[s1] == s3)
if (s1[0] == s3[0])
return true if _is_interleave(s1[1..-1], s2, s3[1..-1])
end
if (s2[0] == s3[0])
return true if _is_interleave(s1, s2[1..-1], s3[1..-1])
end
$dp1[s1] = s3
return false
end
def is_interleave(s1, s2, s3)
$dp1 = Hash.new
return false unless s1.length + s2.length == s3.length
return _is_interleave(s1, s2, s3)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment