TopcoderArchive
Archive
Algorithm problem

JoshString

Problem statement, definition, constraints, and public examples.

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

  1. "bad"
    Returns: "YES"
    The example from the problem statement.
  2. "badbad"
    Returns: "NO"
    "bad" two times. The sum is 7 * 2 = 14. 14 is not a prime number.
  3. "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.
  4. "abcdefghijklmnopqrstuvwxyzaa"
    Returns: "YES"
    Same string but contains 2 extra 'a's. The sum will be 351 + 2 = 353 which is a prime number.
  5. "asdjkhqwaieyajhdjsahjquawyhasdhwauyashjzxdf"
    Returns: "YES"
Back to all problems