TopcoderArchiveVisit Topcoder
SRM25 May 2011
SRM 518

LargestSubsequence

Problem statement, definition, constraints, and public examples.

Problem Statement

For Strings x and y, we say y is a subsequence of x if y can be obtained from x by erasing some (possibly all or none) of the letters in x. For example, "tpcdr" is a subsequence of "topcoder", while "rt" is not.

Given a String s, return the lexicographically largest subsequence of s.

Definition

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

Notes

  • For strings x and y, x is said to be lexicographically larger than y if y is a prefix of x or y has a smaller character than x at the first position where they differ. Order of characters is defined as the order of ASCII codes: 'a' < 'b' < ... < 'z'.

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character in s will be a lowercase letter ('a'-'z').

Examples

  1. "test"
    Returns: "tt"
    All subsequences listed in lexicographical order are "" (empty string), "e", "es", "est", "et", "s", "st", "t", "te", "tes", "test", "tet", "ts", "tst" and "tt". So return "tt".
  2. "a"
    Returns: "a"
    There are only two subsequences, "" and "a".
  3. "example"
    Returns: "xple"
  4. "aquickbrownfoxjumpsoverthelazydog"
    Returns: "zyog"
Back to all problems