Problem p03488 (python)

Fixed submission:
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str

def solve(s: str, x: int, y: int):
    s_list = list(map(len,s.split("T")))
    x_s_list = s_list[0::2]
    y_s_list = s_list[1::2]

    x_dp = [set() for _ in range(len(x_s_list))]
    for index,count in enumerate(x_s_list):
        ## 最初は
        if index == 0:
            x_dp[index].add(count)
            continue
        
        for j in x_dp[index-1]:
            x_dp[index].add(j-count)
            x_dp[index].add(j+count)


    y_dp = [set() for _ in range(len(y_s_list))]
    for index,count in enumerate(y_s_list):
        ## 最初は
        if index == 0:
            y_dp[index].add(count)
            y_dp[index].add(-count)  
            continue
        
        for j in y_dp[index-1]:
            y_dp[index].add(j-count)
            y_dp[index].add(j+count)
    
    if not y_dp:
        if y != 0:
            print(NO)
        else:
            if x in x_dp[-1]:
                print(YES)
            else:
                print(NO)
        return

    if not x_dp:
        if x != 0:
            print(NO)
        else:
            if y in y_dp[-1]:
                print(YES)
            else:
                print(NO)
        return

    if x in x_dp[-1] and y in y_dp[-1]:
        print(YES)
    else:
        print(NO)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    x = int(next(tokens))  # type: int
    y = int(next(tokens))  # type: int
    solve(s, x, y)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str

def solve(s: str, x: int, y: int):
    LENS = len(s)
    s_list = list(map(len,s.split("T")))
    x_s_list = s_list[0::2]
    y_s_list = s_list[1::2]

    x_dp = [[False]*(2*LENS+1) for _ in range(len(x_s_list))]
    ## answer = x_dp[-1][x+LENS]
    for index,count in enumerate(x_s_list):
        ## 最初は
        if index == 0:
            x_dp[index][count+LENS] = True
            continue
        
        for j in range(2*LENS+1):
            if j-count >= 0 and j+count< 2*LENS+1:
                x_dp[index][j] = x_dp[index-1][j-count] or x_dp[index-1][j+count] 
            elif j-count >= 0:
                x_dp[index][j] = x_dp[index-1][j-count]
            elif j+count< 2*LENS+1:
                x_dp[index][j] = x_dp[index-1][j+count]


    y_dp = [[False]*(2*LENS+1) for _ in range(len(y_s_list))]
    for index,count in enumerate(y_s_list):
        ## 最初は
        if index == 0:
            y_dp[index][count+LENS] = True
            y_dp[index][-count+LENS] = True            
            continue
        
        for j in range(2*LENS+1):
            if j-count >= 0 and j+count< 2*LENS+1:
                y_dp[index][j] = y_dp[index-1][j-count] or y_dp[index-1][j+count] 
            elif j-count >= 0:
                y_dp[index][j] = y_dp[index-1][j-count]
            elif j+count< 2*LENS+1:
                y_dp[index][j] = y_dp[index-1][j+count]
    

    if x_dp[-1][x+LENS] and y_dp[-1][y+LENS]:
        print(YES)
    else:
        print(NO)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    x = int(next(tokens))  # type: int
    y = int(next(tokens))  # type: int
    solve(s, x, y)

if __name__ == '__main__':
    main()