Statistics

Problem Statement for "MagicalStringDiv2"

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.

Definition

Class:
MagicalStringDiv2
Method:
minimalMoves
Parameters:
String
Returns:
int
Method signature:
int minimalMoves(String S)
(be sure your method is public)

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

  1. ">><<><"

    Returns: 2

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

  2. ">>>><<<<"

    Returns: 0

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

  3. "<<>>"

    Returns: 4

  4. "<><<<>>>>><<>>>>><>><<<>><><><><<><<<<<><<>>><><><"

    Returns: 20

  5. "><"

    Returns: 0

  6. "<<"

    Returns: 1

  7. "<><>"

    Returns: 2

  8. "<<<<"

    Returns: 2

  9. "<><>>>"

    Returns: 5

  10. ">>>><<"

    Returns: 1

  11. "<<><>><<"

    Returns: 5

  12. "<<<>><>>"

    Returns: 6

  13. ">><>>>>><>"

    Returns: 5

  14. ">>>><<><><"

    Returns: 3

  15. "<><>>>>>>>>>"

    Returns: 8

  16. "<>><>><<><><"

    Returns: 4

  17. "><><<<>>"

    Returns: 4

  18. ">>>>><><><>><>>>><><<<"

    Returns: 9

  19. ">><<<>><<>><"

    Returns: 6

  20. "><><<><<<<><><<>><"

    Returns: 10

  21. "<<<>><><>><<>>><><>>><<>>>><>>><<<"

    Returns: 18

  22. "<<>>><>><>>>><<<<>><<<<><<><<<<<><<<<>>>>><><<"

    Returns: 21

  23. "><<><>><>>><><>>><<><>><<><<><>>>><>><"

    Returns: 19

  24. "<><>><><><<<<<>>><<><>><><<>><><<<>>>><>><><>><><>"

    Returns: 27

  25. "><<<><<>><><<<<<><>>><<>><>><<>><<><<><>>><<><><<>"

    Returns: 26

  26. "<<>>><>><>>>>>>>><<><>><<<><>><<"

    Returns: 11

  27. "<<<<<<<<<<<<<<>>>>>>>>>>>>>>"

    Returns: 28

  28. "<<<>><><<>><><<>"

    Returns: 9

  29. "<<<<<>>>>>"

    Returns: 10

  30. "><<<><<><<<<<<"

    Returns: 6

  31. "<<><<<<>><<<<<<><>>><><><>"

    Returns: 17

  32. "><<<><>><><><<<>><>><<"

    Returns: 11

  33. "<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>"

    Returns: 48

  34. ">>><>>>>><><><<<<><>><<<><"

    Returns: 7

  35. "<>>><<<<<<>><<><><><<><<"

    Returns: 11

  36. ">>>>>>>>><<>"

    Returns: 4

  37. ">><<>><<><<<<<>>>>><>>><><<><<<<<<<><>"

    Returns: 16

  38. "<<<>><<<"

    Returns: 4

  39. "><>><<><<><<<>><<<>>><<<<>><<<><<>>><<"

    Returns: 19

  40. "<><<<><>>>><<<<><>><"

    Returns: 9

  41. "<<<>>>><<<>>>>><><><<<<<<>><<>><<<>>><><>><><>><><"

    Returns: 28

  42. ">><>><<<>><><><>>><<>><<<<>><<>>><><<<>>>><<<><<>>"

    Returns: 25

  43. "<>>>>>><<<<<><><><><<><>>><<><<<>><<>><<<>>><<<><<"

    Returns: 22

  44. "<><<>>><<><><><><<<><>>><<>><<<><<>><><<>>><<>><<<"

    Returns: 24

  45. ">>><<>><><<><<<<><<<<<<>><>><><<<<<>><<<<><<><><<>"

    Returns: 24

  46. "<<<<<><<<<<><>><<<><<>><<<><><><><><>><><><<><<><>"

    Returns: 30

  47. "<><><<><><<><><>><<><<><<<><>><<><<<>><<<<><<<><<>"

    Returns: 24

  48. ">><<><><>>><<<<<>><><<<<><<><><<<><><><>><<>>>><<<"

    Returns: 25

  49. "><<><<><><<<<<>>>>><><><<><<>>><><>><><><>><<<<>>>"

    Returns: 28

  50. "<>><<><><><<><><<>><><<>><<><><<<<<<<>>><<><<><<>>"

    Returns: 22

  51. "<><><><<><><<><<><<<>><<><<><>><<<>><><>><<>><<<<>"

    Returns: 26

  52. ">>><<<><><<>>>>><<<><><>><<>><<<><><><<>><<><><<<>"

    Returns: 21

  53. "<>>><<<><><>>><>>>>><>><<><<<><><<>><>><<><>>><><>"

    Returns: 23

  54. ">><><>><<><><<<<>>>><><>>>>>>>>><><<<<>><>><<><<><"

    Returns: 25

  55. "><>>><>><<><>>>><><>>>><<<<<<<><<<><<>>><>>>><<<<<"

    Returns: 18

  56. "<<<<>>>><><>>>><><<><<<>>>><><<>><<><<<<<<><<>>>><"

    Returns: 23

  57. ">>"

    Returns: 1

  58. ">>>>"

    Returns: 2

  59. ">>><"

    Returns: 1

  60. ">>>>>>>>"

    Returns: 4

  61. "<<<>>>"

    Returns: 6

  62. "><<>>>"

    Returns: 5

  63. "<><><>"

    Returns: 4

  64. "<<<<<<<>>>>>>>"

    Returns: 14


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: