Commit 0c1fc93

mo <mokha@cisco.com>
2017-06-04 23:28:45
early exit if node equals computed max.
1 parent 1fb545c
Changed files (1)
spec/swap_lex_order_spec.rb
@@ -37,11 +37,13 @@ describe "swap_lex_order" do
   def swap_lex_order(string, pairs)
     queue = [string]
     max_node = string
+    computed_max = string.chars.sort.reverse.join
 
     visited = {}
     until queue.empty?
       node = queue.shift
       next if visited.key?(node)
+      return node if node == computed_max
 
       if (node <=> max_node) > 0
         max_node = node