Solve substrings problem with iteration and recursion in ruby.