tkang1
BAN USERdef longest_substr_with_uniq_char(str)
return "" if (str.size == 0)
splits = str.split("")
uniq_char_str = splits[0]
longest = uniq_char_str
splits[1..-1].each do |ch|
if (!uniq_char_str.include?(ch))
uniq_char_str << ch
else
uniq_char_str = ch
end
if (uniq_char_str.length > longest.length)
longest = uniq_char_str
end
end
longest
end
def commonAncestor(node1, node2)
ancestors1 = listAncestors(node1)
ancestors2 = listAncestors(node2)
common = null
while (ancestors1.isNotEmpty and ancestors2.isNotEmpty) do
a1 = ancestors1.dequeue
a2 = ancestors2.dequeue
if (a1!= a2)
break
common = a1 # or a2
end
return common
end
def listAncestors(node)
ancestors = Queue.new
current = node
while (true) do
if (current == rootNode)
break
ancestors.enqueue(current.parent)
current = current.parent
end
return ancestors
end
- tkang1 January 22, 2015