problems / 237

Mirror Runs of the Season (easy version)

easy strings palindromes brute-force 1000 ms · 256 MB

Mirror Runs of the Season (easy version)

Every match a team plays in the league ends in one of three results: a win (W), a draw (D) or a loss (L). Writing a team's results in the order the matches were played gives its season as a string of n letters.

The league commentator hands out a mirror run award for every stretch of two or more consecutive matches whose results read the same forwards and backwards. For example, in the season WLLWD the stretches LL (matches 2–3) and WLLW (matches 1–4) are mirror runs, but LLW and WD are not.

Every stretch counts on its own: two stretches are different if they start or end at a different match, even when their results are the same, and a stretch counts even if it overlaps or sits inside another mirror run. A single match is never a mirror run.

Given one team's season, count its mirror runs.

Input

The first line contains an integer n (1 ≤ n ≤ 100), the number of matches.

The second line contains a string s of exactly n characters, each W, D or L: the results in the order the matches were played.

Output

Print one integer: the number of mirror runs in the season.

Notes

In the first example the season is WLLWDWD. Its mirror runs are LL (matches 2–3), WLLW (matches 1–4), WDW (matches 4–6) and DWD (matches 5–7), so the answer is 4. The first two are nested and the last two overlap; all of them count.

In the second example every stretch of two or more draws is a mirror run: three of length 2, two of length 3 and one of length 4, for 6 in total.

In the third example the season has a single match, so there are no mirror runs.

Example 1

Input

7
WLLWDWD

Output

4

Example 2

Input

4
DDDD

Output

6

Example 3

Input

1
L

Output

0

Hints · reveal one at a time if you're stuck

Hint 1

With at most 100 matches, how many stretches of two or more consecutive matches are there? Few enough to look at every one of them.

Hint 2

A mirror run is just a palindrome of length at least 2, and checking whether one stretch is a palindrome is cheap. Count every position separately (DDDD has six mirror runs), and don't forget even lengths like LL.

report an issue Prefer your own editor? sooks prep 237 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.