hackerrank/string_manipulation/common_child/main.rb
# Complete the commonChild function below.
Complete the commonChild function below.
Complete the commonChild function below .
def common_child(s1, s2)
  s1_a = s1.chars
  s2_a = s2.chars
  intersection = s1_a & s2_a
  s1_clean = s1_a.select { |c| intersection.include?(c) }
  s2_clean = s2_a.select { |c| intersection.include?(c) }
  return 0 if s1_clean.empty? || s2_clean.empty?
  l = s1_clean.length < s2_clean.length ? s1_clean.length : s2_clean.length
  l.downto(1).each do |i|
    s1_comb = s1_clean.combination(i).to_a
    counter = {}
    (0..s1_comb.length - 1).each do |j|
      counter[s1_comb[j].join] = counter[s1_comb[j].join].to_i + 1
    end
    s2_comb = s2_clean.combination(i).to_a
    (0..s2_comb.length - 1).each do |j|
      next if counter[s2_comb[j].join].nil?
      return i
    end
  end
  0
end
def common_child ( s1 , s2 ) s1_a = s1 . chars s2_a = s2 . chars intersection = s1_a & s2_a s1_clean = s1_a . select { | c | intersection . include? ( c ) } s2_clean = s2_a . select { | c | intersection . include? ( c ) } return 0 if s1_clean . empty? || s2_clean . empty? l = s1_clean . length < s2_clean . length ? s1_clean . length : s2_clean . length l . downto ( 1 ) . each do | i | s1_comb = s1_clean . combination ( i ) . to_a counter = { } ( 0 .. s1_comb . length - 1 ) . each do | j | counter [ s1_comb [ j ] . join ] = counter [ s1_comb [ j ] . join ] . to_i + 1 end s2_comb = s2_clean . combination ( i ) . to_a ( 0 .. s2_comb . length - 1 ) . each do | j | next if counter [ s2_comb [ j ] . join ] . nil? return i end end 0 end
Complete the commonChild function below.
Complete the commonChild function below .
# Remove non-shared characters # Find longest shared combination length
s1
s2