**Que:** <https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap>

**What I tried & issue faced:** I tried a recursive approach to solve this problem. Currently, unable to find the overlapping subproblems. Can anyone help me how can I optimize, memoize and modify/update my recursion approach?. (I am thinking my approach is Wrong)

**approach:** Either the word is in the current line(if and only if space is left) or will be in the new line.

**Code Flow:**

```
Input text = "Cat is on Floor" .
I stored len in an array = [3,2,2,5]

ans(a,b,c) : a: index, b: line (currently no use), c: current line sum

base condn if all elements are over a>=size: return pow(spaceleft,2)

will return min( subproblem(word in current line),subproblem(word in new line)+pow(spaceleft,2))

Initially :     ans(1,1,3)
          ans(2,1,6)     ans(2,2,2)
      ans(3,2,2)      ans(3,2,5)  ans(3,3,2)
      ans(4,3,5)      ans(4,3,5)  ans(4,4,5)

```

The below code is in python3.x :

```
n=6
arr=[3,2,2,5]
size = 4
def ans(a,b,c):
    if(a>=size):
        return pow((n-c),2);
    if(c+arr[a]+1 > n):
        return (pow((n-c),2)+ans(a+1,b+1,arr[a]))
    return (min(ans(a+1,b,c+arr[a]+1),pow((n-c),2)+ans(a+1,b+1,arr[a])))
print(ans(1,1,3))

```

Thanks in advance for giving your valuable time and helping me....