TopCoder problem "AppleWord" used in SRM 456 (Division II Level One)



Problem Statement

    An Apple Word is a word that consists of only the letters A, P, L, and E, in that exact relative order. There must be exactly one A, two or more Ps, exactly one L and exactly one E. Case does not matter. For example, "Apple" and "apPpPPlE" are Apple Words, while "APLE", "Apples", "Aplpe" and "coconut" are not.



You are given a String word which you must turn into an Apple Word. For each character in word, you can either replace it with a different letter or leave it unchanged. No other operations, like inserting new characters or deleting existing characters, are allowed. Return the minimal number of characters you must replace to get an Apple Word. If it's impossible, return -1.
 

Definition

    
Class:AppleWord
Method:minRep
Parameters:String
Returns:int
Method signature:int minRep(String word)
(be sure your method is public)
    
 

Constraints

-word will contain between 1 and 50 characters, inclusive.
-Each character in word will be an uppercase letter ('A'-'Z') or a lowercase letter ('a'-'z').
 

Examples

0)
    
"Apple"
Returns: 0
This is an Apple Word.
1)
    
"apPpPPlE"
Returns: 0
This is also an Apple Word.
2)
    
"APLE"
Returns: -1
An Apple Word must contain at least two 'P's.
3)
    
"TopCoder"
Returns: 7
For example, if you replace 7 characters, you can get "Appppple".

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13909&pm=10711

Writer:

rng_58

Testers:

PabloGilberto , misof , ivan_metelsky

Problem categories:

Simple Search, Iteration