Advertisement
brewersfan1976

strings_construction.rb

Oct 1st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.27 KB | None | 0 0
  1. def stringsConstruction(a, b)
  2.     a_count = a.chars.group_by(&:chr).map {|k, v| [k, v.size]}
  3.     b_count = b.chars.group_by(&:chr).map {|k, v| [k, v.size]}
  4.     array_a = Array.new
  5.     array_b = Array.new
  6.     array_a = a.split("")
  7.     array_b = b.split("")
  8.     best = -1
  9.    
  10.     if (array_a & array_b).length == 0 then
  11.        return 0
  12.     end
  13.        
  14.     b_new = Array.new(26){Array.new(2)}
  15.     x = 0
  16.     y = 0
  17.     z = 0
  18.        
  19.     while (x < b_count.length)
  20.           while (y < a_count.length)
  21.                 if b_count[x][0] == a_count[y][0] then
  22.                    b_new[z][0] = b_count[x][0]
  23.                    b_new[z][1] = b_count[x][1]
  24.                    z = z + 1
  25.                 end
  26.                    
  27.                 y = y + 1
  28.           end
  29.              
  30.           x = x + 1
  31.           y = 0
  32.     end
  33.        
  34.     a_count = a_count.sort
  35.     b_new = b_new - [[nil, nil]]
  36.     b_new = b_new.sort
  37.    
  38.     if a_count.length != b_new.length then
  39.        return 0
  40.     end
  41.        
  42.     x = 0
  43.        
  44.     while (x < a_count.length)
  45.           cur = b_new[x][1] / a_count[x][1]
  46.          
  47.           if best == -1 or cur < best then
  48.              best = cur
  49.           end
  50.              
  51.           x = x + 1
  52.     end
  53.        
  54.     return best
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement