Problem:
Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.HashSet;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String s = sc.next();
        int x = sc.nextInt();
        int y = sc.nextInt();

        System.out.println(solve(s, x, y) ? "Yes" : "No");

        sc.close();
    }

    static boolean solve(String s, int x, int y) {
        Set<State> xStates = new HashSet<>();
        xStates.add(new State(0, 1));

        Set<State> yStates = new HashSet<>();
        yStates.add(new State(0, 1));

        boolean xOrY = true;
        for (char command : s.toCharArray()) {
            if (command == 'F') {
                if (xOrY) {
                    xStates = xStates.stream().map(xState -> new State(xState.value + xState.offset, xState.offset))
                            .collect(Collectors.toSet());
                } else {
                    yStates = yStates.stream().map(yState -> new State(yState.value + yState.offset, yState.offset))
                            .collect(Collectors.toSet());
                }
            } else {
                xOrY = !xOrY;

                if (xOrY) {
                    xStates.addAll(xStates.stream().map(xState -> new State(xState.value, -xState.offset))
                            .collect(Collectors.toList()));
                } else {
                    yStates.addAll(yStates.stream().map(yState -> new State(yState.value, -yState.offset))
                            .collect(Collectors.toList()));
                }
            }
        }

        return xStates.stream().anyMatch(xState -> xState.value == x)
                && yStates.stream().anyMatch(yState -> yState.value == y);
    }
}

class State {
    int value;
    int offset;

    State(int value, int offset) {
        this.value = value;
        this.offset = offset;
    }

    @Override
    public int hashCode() {
        return Objects.hash(value, offset);
    }

    @Override
    public boolean equals(Object obj) {
        State other = (State) obj;

        return value == other.value && offset == other.offset;
    }
}