-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstrings.rb
34 lines (29 loc) · 862 Bytes
/
substrings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
dictionary = ["below","down","go","going","horn","how","howdy","it","i","low","own","part","partner","sit"]
def count_times_sub_str_appear(str, substr)
# substring matching
str.scan(/(?=#{substr})/).count
end
def substrings(string, dictionary)
# taking string and converting all to downcase
given_string = string.downcase
array = []
hash = {}
# looping over whole dictionary
dictionary.each do |word|
# checking if current word match
given_string.match(word)
# extract actual string matched
result = $&
# push it in array
array.push(result)
# remove nil values
array.compact!
end
# loop over array
array.each do |word|
# add each element in array as key its occurrence as value
hash[word] = count_times_sub_str_appear(given_string, word)
end
puts hash
end
substrings("below", dictionary)