Skip to content

Instantly share code, notes, and snippets.

@BEaStia
Last active November 16, 2016 10:22
Show Gist options
  • Select an option

  • Save BEaStia/74338edc4e40b11d16a688a5fd2b549b to your computer and use it in GitHub Desktop.

Select an option

Save BEaStia/74338edc4e40b11d16a688a5fd2b549b to your computer and use it in GitHub Desktop.
str1 = 'abc def fegh '
str2 = 'feg qwe eerwr feg'
class WordDivider
MAX_WORD_SIZE = 15
MIN_WORD_SIZE = 1
attr_accessor :text, :substrings
def initialize(string)
@text = string
@substrings = substringify!
end
def intersect(string)
substrings.select { |x| string.include?(x) }.sort_by(&:length).reverse
end
private
def substringify!
((MIN_WORD_SIZE - 1)..MAX_WORD_SIZE).each_with_object([]) do |i, arr|
(0..(text.length - i)).each do |j|
string = text[j..j + i]
next if WordValidator.new.validate(string) || arr.include?(string)
arr << string unless arr.include?(string)
end
end
end
end
class WordValidator
def validate(str)
str.empty? ||(str =~ /\W/)
end
end
str_array = WordDivider.new(str1).intersect(str2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment