Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def stringsConstruction(a, b)
- a_count = a.chars.group_by(&:chr).map {|k, v| [k, v.size]}
- b_count = b.chars.group_by(&:chr).map {|k, v| [k, v.size]}
- array_a = Array.new
- array_b = Array.new
- array_a = a.split("")
- array_b = b.split("")
- best = -1
- if (array_a & array_b).length == 0 then
- return 0
- end
- b_new = Array.new(26){Array.new(2)}
- x = 0
- y = 0
- z = 0
- while (x < b_count.length)
- while (y < a_count.length)
- if b_count[x][0] == a_count[y][0] then
- b_new[z][0] = b_count[x][0]
- b_new[z][1] = b_count[x][1]
- z = z + 1
- end
- y = y + 1
- end
- x = x + 1
- y = 0
- end
- a_count = a_count.sort
- b_new = b_new - [[nil, nil]]
- b_new = b_new.sort
- if a_count.length != b_new.length then
- return 0
- end
- x = 0
- while (x < a_count.length)
- cur = b_new[x][1] / a_count[x][1]
- if best == -1 or cur < best then
- best = cur
- end
- x = x + 1
- end
- return best
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement