Sha256: 5fd4e30833f4eb3cdab39f0a4ceef315aa6131dedb8ce731114b21d40476945b

Contents?: true

Size: 712 Bytes

Versions: 27

Compression:

Stored size: 712 Bytes

Contents

# Return the longest common prefix between two strings.
def longest_common_prefix(strings)
  p = nil
  strings.map{|s|s.size}.min.times do |c|
    if strings.map{|s|s[c]}.uniq.size == 1
      p = c
    else
      break
    end
  end

  strings.first[0..p] unless p.nil?
end


def longest_common_subsequence(s1, s2)

  num = Array.new(s1.size) { Array.new(s2.size) }
  len, ans = 0

  s1.chars.each_with_index do |l1, i|
    s2.chars.each_with_index do |l2, j|
      unless l1==l2
        num[i][j]=0
      else
        if (i==0 || j==0)
          num[i][j] = 1
        else
          num[i][j] = 1 + num[i-1][j-1]
        end
        len = ans = num[i][j] if num[i][j] > len
      end
    end
  end

  ans

end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
epitools-0.5.136 lib/epitools/lcs.rb
epitools-0.5.134 lib/epitools/lcs.rb
epitools-0.5.133 lib/epitools/lcs.rb
epitools-0.5.131 lib/epitools/lcs.rb
epitools-0.5.130 lib/epitools/lcs.rb
epitools-0.5.129 lib/epitools/lcs.rb
epitools-0.5.128 lib/epitools/lcs.rb
epitools-0.5.126 lib/epitools/lcs.rb
epitools-0.5.125 lib/epitools/lcs.rb
epitools-0.5.124 lib/epitools/lcs.rb
epitools-0.5.123 lib/epitools/lcs.rb
epitools-0.5.122 lib/epitools/lcs.rb
epitools-0.5.121 lib/epitools/lcs.rb
epitools-0.5.119 lib/epitools/lcs.rb
epitools-0.5.118 lib/epitools/lcs.rb
epitools-0.5.116 lib/epitools/lcs.rb
epitools-0.5.115 lib/epitools/lcs.rb
epitools-0.5.114 lib/epitools/lcs.rb
epitools-0.5.113 lib/epitools/lcs.rb
epitools-0.5.112 lib/epitools/lcs.rb