TopCoder problem "JoshString" used in TCHS SRM 38 (Division I Level One)



Problem Statement

    Josh said every string with a certain property is his string. The property that Josh is talking about is that the sum of the letters of the string has to be a prime number. The string will contain only lowercase letters ('a'-'z'). For each letter you add to the sum the 1-based position of the letter in the alphabet. For 'a' you add 1, for 'b' you add '2', and so on. For example, the sum of the letters in "bad" is 2 + 1 + 4 = 7, and 7 is prime, so therefore, "bad" is Josh's string. Given a String s, return "YES" if it is Josh's string or "NO" otherwise (all quotes for clarity only).
 

Definition

    
Class:JoshString
Method:isJoshString
Parameters:String
Returns:String
Method signature:String isJoshString(String s)
(be sure your method is public)
    
 

Constraints

-s will contain only lowercase letters ('a'-'z').
-s will contain between 2 and 50 characters, inclusive.
 

Examples

0)
    
"bad"
Returns: "YES"
The example from the problem statement.
1)
    
"badbad"
Returns: "NO"
"bad" two times. The sum is 7 * 2 = 14. 14 is not a prime number.
2)
    
"abcdefghijklmnopqrstuvwxyz"
Returns: "NO"
The whole alphabet. The sum will be 1 + 2 + 3 + .. + 26 = 351. 351 is not a prime number because it is divisible by 3.
3)
    
"abcdefghijklmnopqrstuvwxyzaa"
Returns: "YES"
Same string but contains 2 extra 'a's. The sum will be 351 + 2 = 353 which is a prime number.
4)
    
"asdjkhqwaieyajhdjsahjquawyhasdhwauyashjzxdf"
Returns: "YES"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10779&pm=8094

Writer:

vlad_D

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Brute Force, Simple Math, String Parsing