TopcoderArchive
Archive
Algorithm problem

ReverseHash

Problem statement, definition, constraints, and public examples.

Problem Statement

You have uncovered the hashing algorithm for passwords at a particular site. This hashing algorithm is brutally simple, as shown by the code below:
   long h = 0 ;
   for (char c in s)
      h = (h * 37 + c) % (2^32) ;
   return h ;
(A long here is large enough so the arithmetic does not overflow.) You believe that most customers are using passwords composed of just uppercase letters (A-Z). Given an integer hash code, you wish to return the shortest possible password consisting of only uppercase letters that hashes to that value. If there is more than one such password, return the one that comes first alphabetically. The letter A has an ASCII code of 65, and the letter Z has an ASCII code of 90.

Definition

Class:
ReverseHash
Method:
unhash
Parameters:
int
Returns:
String
Method signature:
String unhash(int hashCode)
(be sure your method is public)

Constraints

  • hashCode will be between 1 and 2,147,483,647, inclusive.

Examples

  1. 65
    Returns: "A"
    The hash code of a single-character string is just the ASCII value of that character.
  2. 97
    Returns: "FGSHWIA"
    The character coded at ASCII 97 is 'a', but we must find a word consisting of only uppercase letters to return.
  3. 129666101
    Returns: "CODER"
  4. 1378690842
    Returns: "PASSWD"
    This is not the best password in the world.
Back to all problems