What could be the more efficient way of solving the 'sequence land' problem?

I have been trying to solve the problem SEQUENCE LAND and I have got ac but the complexity of My Solution is O(N^2).
I would like to know what would be the more efficient way of solving this problem. Constraints in this problem were fairly small, my solution would have not worked otherwise.