TLE : stuck wtih the time limit

Can someone suggest to me how to reduce time complexity for the solution…?

class Solution:
# @param A : list of integers
# @param B : list of integers
# @return a list of integers
def solve(self, A, B):
G = []
n = len(A)
for i in range(n):
for j in range(i+1, n+1):
# print(A[i:j])
# print(G)
mod = 1000000007
for i in range(len(G)):
f = 1
j = 1
if G[i]%j == 0:
if G[i]/j==j:
f = j
f = f
j += 1
G[i] = f%mod
# print(G)
G.sort(reverse = True)
# print(G)
ans = []
for i in B:
return ans

instead of processing the string for all sub-strings which will take O(n2) time and then finding the corresponding max will take O(n) so overall O(n3) just pre-process and store in left array and right array the indexes till where ith elements acts as max on that side.
Then the number of sum-strings for which the ith index will be max can be (i-left[i])*(right[i]-i) this will just take O(n) time.
Finally instead of putting all values in G and sorting take distinct values and their count and sort then do binary search for values in B this will save space.

Click here to start solving coding interview questions