TopcoderArchiveVisit Topcoder
SRM22 Dec 2016
SRM 705 Sponsored By Blizzard

AlphabetOrderDiv2

Problem statement, definition, constraints, and public examples.

Problem Statement

The ancient civilization of Nlogonia used the same 26 letters as modern English: 'a'-'z'. However, we do not know the order in which these letters appeared in the Nlogonian alphabet.
One particular custom in Nlogonia was that in a good word the letters appear in non-decreasing order. For example, in English the word "ciel" is not a good word because in the alphabet 'i' is after 'e'. The word "ceil" is a good word because 'c' <= 'e' <= 'i' <= 'l'.
You are given the String[] words. Each element of words is a nonempty string of lowercase English letters. Return "Possible" if it is possible that all elements of words were good words in Nlogonian, and "Impossible" otherwise.
In other words, return "Possible" if and only if there is at least one possible Nlogonian alphabet such that the letters of each word in words are in non-decreasing alphabetical order.

Definition

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

Constraints

  • words has between 2 and 100 elements inclusive.
  • The size of each element of words will be between 1 and 100 inclusive.
  • Elements of words contains only English lowercase letters from 'a' to 'z'.

Examples

  1. {"single","round","match"}
    Returns: "Possible"
    One possible Nlogonian alphabet is "bfjkmapqrositchundglevwxyz".
  2. {"topcoder","topcoder"}
    Returns: "Impossible"
    The word "topcoder" can never be a good word. The character 'o' cannot be both before 'p' and after 'p' in the alphabet.
  3. {"algorithm", "contest"}
    Returns: "Impossible"
  4. {"pink","floyd"}
    Returns: "Possible"
  5. {"gimnasia","y","esgrima","la","plata"}
    Returns: "Impossible"
  6. {"hello","hello"}
    Returns: "Possible"
    This is a good word for any alphabet in which the letters 'h', 'e', 'l', and 'o' appear in this order.
  7. {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}
    Returns: "Possible"
    In this case the English alphabet is one of the valid permutations.
  8. {"abc","bca"}
    Returns: "Impossible"
    'a' must come before 'c' (because of the first name) and after 'c' (because of the second name) and that is a contradiction. Thus, there is no valid solution.
  9. {"aaaaa","eeeee","iiiii","ooooo","uuuuu"} 
    Returns: "Possible"
    Any order is valid in this case
  10. {"ab","bc","ca"}
    Returns: "Impossible"
Back to all problems