TopcoderArchiveVisit Topcoder
SRM24 May 2008
SRM 402

WordAbbreviation

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a String[] words, each element of which is a single word. Return a String[] where the i-th element is the abbrevation for the i-th word. The abbreviation for a word is its shortest non-empty prefix that is not a prefix of any other given word. The constraints will guarantee that it is possible to find an abbreviation for all the given words.

Definition

Class:
WordAbbreviation
Method:
getAbbreviations
Parameters:
String[]
Returns:
String[]
Method signature:
String[] getAbbreviations(String[] words)
(be sure your method is public)

Notes

  • A string s1 is called a prefix of string s2 if and only if s1 can be obtained by removing zero or more characters from the end of s2.

Constraints

  • words will contain between 1 and 50 elements, inclusive.
  • Each element of words will contain between 1 and 50 characters, inclusive.
  • Each element of words will only contain lowercase letters ('a'-'z').
  • No element of words will be a prefix of another element of words.

Examples

  1. {"abc","def","ghi"}
    Returns: {"a", "d", "g" }
    A single character is enough.
  2. {"aaab","aaac","aaad"}
    Returns: {"aaab", "aaac", "aaad" }
    It's possible that the abbreviation is the same as the original word.
  3. {"top","coder","contest"}
    Returns: {"t", "cod", "con" }
  4. {
     "bababaaaaa",
     "baaabaababa",
     "bbabaaabbaaabbabaabaabbbbbaabb",
     "aaababababbbbababbbaabaaaaaaaabbabbbaaab",
     "baaaaabaababbbaabbbabbababbbabbbbbbbbab"
    }
    Returns: {"bab", "baaab", "bb", "a", "baaaa" }
  5. {"oneword"}
    Returns: {"o" }
Back to all problems