Connection Status:
Competition Arena > MagicalStringDiv2
SRM 609 · 2013-12-22 · by semiexp · Simple Search, Iteration
Class Name: MagicalStringDiv2
Return Type: int
Method Name: minimalMoves
Arg Types: (string)
Problem Statement

Problem Statement

Magical Girl Illy uses "magical strings" to cast spells. For her, a string X is magical if and only if there exists a positive integer k such that X is composed of k consecutive '>' characters followed by k consecutive '<' characters.
Once Illy picked up a String S. The length of S was even, and each character of S was either '<' or '>'. Illy wants to change S into a magical string. In each step, she can change a single '>' to a '<' or vice versa. Compute and return the smallest number of steps in which she can change S into a magical string.

Constraints

  • S will contain between 2 and 50 characters, inclusive.
  • S will contain even number of characters.
  • Each character of S will be '<' or '>'.
Examples
0)
">><<><"
Returns: 2

She needs to change character 2 (0-based index) from '<' to '>', and character 4 from '>' to '<'.

1)
">>>><<<<"
Returns: 0

S is already a magical string, so no changes are needed.

2)
"<<>>"
Returns: 4
3)
"<><<<>>>>><<>>>>><>><<<>><><><><<><<<<<><<>>><><><"
Returns: 20
4)
"><"
Returns: 0

Submissions are judged against all 64 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class MagicalStringDiv2 with a public method int minimalMoves(string S) · 64 test cases · 2 s / 256 MB per case

Submitting as anonymous