Greedy and Solve reversely
1 min readDec 8, 2019
Lots of problems will be much easier if solve them reversely. Here is one example from leetcode.
936. Stamping The Sequence
The solution is given here:
One python code is given here:
Solve reversely
844. Backspace String Compare
def backspaceCompare(self, S, T):
i, j = len(S) - 1, len(T) - 1
backS = backT = 0
while True:
while i >= 0 and (backS or S[i] == '#'):
backS += 1 if S[i] == '#' else -1
i -= 1
while j >= 0 and (backT or T[j] == '#'):
backT += 1 if T[j] == '#' else -1
j -= 1
if not (i >= 0 and j >= 0 and S[i] == T[j]):
return i == j == -1
i, j = i - 1, j - 1
Thanks for reading.