TopCoder problem "DiamondHunt" used in SRM 346 (Division II Level One)



Problem Statement

    

You are a diamond hunter looking for diamonds in a peculiar mine. This mine is a String of '<' and '>' characters, and each diamond is a substring of the form "<>". Each time you find a diamond, you remove it and the residual mine is updated by removing the 2 characters from the String.

For example, if you have a mine like "><<><>>><", you can start by removing the first appearance of "<>" to get "><<>>><", then remove the only remaining diamond to get "><>><". Note that this produces a new diamond which you can remove to get ">><". Since there are no diamonds left, your expedition is done.

Given a String mine, return the number of diamonds that can be found. Note that the order in which you remove simultaneous appearances of diamonds is irrelevant; any order will lead to the same result.

 

Definition

    
Class:DiamondHunt
Method:countDiamonds
Parameters:String
Returns:int
Method signature:int countDiamonds(String mine)
(be sure your method is public)
    
 

Constraints

-mine will contain between 1 and 50 characters, inclusive.
-Each character of mine will be either '<' or '>'.
 

Examples

0)
    
"><<><>>><"
Returns: 3
The example from the problem statement.
1)
    
">>>><<"
Returns: 0
No diamonds here.
2)
    
"<<<<<<<<<>>>>>>>>>"
Returns: 9
3)
    
"><<><><<>>>><<>><<><<>><<<>>>>>><<<"
Returns: 14

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7681

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10670&pm=7681

Writer:

soul-net

Testers:

PabloGilberto , brett1479 , Yarin , Olexiy

Problem categories:

Simulation, String Manipulation