I am trying to solve "the grid search" problem on hacker rank using functional programming. Please view the problem description on hackerrank. https://www.hackerrank.com/challenges/the-grid-search/problem
I want to use only recursion and functional programming primitives such as map, filter, etc. My current solution checks to see if it can find the pattern at the start of the array, if it can't, then I call recursively on the tail of the array. I came up with the following:
def checkLine(str1, str2):
return str1 in str2
def checkGroup(group1, group2):
return False not in list(map(lambda x: checkLine(x[0], x[1]), zip(group1, group2)))
def gridSearch(G, P):
# Write your code here
if len(G) < len(P):
return "NO"
if checkGroup(P, G):
return "YES"
else:
return gridSearch(G[1:], P)
The issue is that I am running into stack overflow when both the array is very large. I know you can avoid stack overflow by using tail recursion but I'm not quite sure how to accomplish that here. Can anyone give an example of how solve this problem functionally but also avoid stack overflow?