Statistics

Problem Statement for "Twain"

Problem Statement

Spelling in the English language doesn't make sense. Congress has enacted a plan for orthographical reform (loosely based on Mark Twain's plan for the improvement of English spelling) which will change the spelling of words in the English language gradually over the next 7 years. The plan is as follows:

Year 1:
  1. - If a word starts with "x", replace the "x" with a "z".
  2. - Change all remaining "x"s to "ks"s.
Year 2:
  1. - Change all "y"s to "i"s.
Year 3:
  1. - If a "c" is directly followed by an "e" or "i", change the "c" to an "s".
Year 4:
  1. - If a "c" is directly followed by a "k", remove the "c". Keep applying this rule as necessary (Example: "cck" becomes "k".)
Year 5:
  1. - If a word starts with "sch", change the "sch" to a "sk".
  2. - If a "ch" is directly followed by an "r", change the "ch" to a "k".
  3. - After applying the above rules, change all "c"s that are not directly followed by an "h", to a "k". (This includes all "c"s that are the last letter of a word.)
Year 6:
  1. - If a word starts with "kn" change the "kn" to an "n".
Year 7:
  1. - Change all double consonants of the same letter to a single consonant. A consonant is any letter that is not one of "a, e, i, o, u." (Example: "apple" becomes "aple"). Keep applying this rule as necessary (Example: "zzz" becomes "z".)

The plan requires that rules for each year are followed in the order they are presented, and changes for each year occur after all the changes from previous years.

Write a class Twain, which contains a method getNewSpelling. getNewSpelling takes as parameters an int year representing the number of years that have passed since the plan to improve the English language began, and a String phrase representing the English phrase to convert. For the purposes of the plan, a word is a sequence of lowercase letters ('a'-'z') bounded by spaces or the start/end of phrase. The method returns a String representing the converted phrase.

Definition

Class:
Twain
Method:
getNewSpelling
Parameters:
int, String
Returns:
String
Method signature:
String getNewSpelling(int year, String phrase)
(be sure your method is public)

Constraints

  • year will be between 0 and 7, inclusive
  • phrase will be between 0 and 50 characters, inclusive
  • phrase will contain only lowercase letters ('a'-'z') and spaces (' ').
  • phrase will not contain three or more of the same consonant in a row

Examples

  1. 1

    "i fixed the chrome xerox by the cyclical church"

    Returns: "i fiksed the chrome zeroks by the cyclical church"

    In year 1, the first "x" in "xerox" is changed to a "z". Then, the "x"s in "fixed" and "zerox" are changed to "ks"s.

  2. 2

    "i fixed the chrome xerox by the cyclical church"

    Returns: "i fiksed the chrome zeroks bi the ciclical church"

    In year 2, the "y"s in "by" and "cyclical" are changed to "i"s.

  3. 0

    "this is unchanged"

    Returns: "this is unchanged"

    Since the year is 0, no changes occur.

  4. 7

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sk n z zskrks nchk sik zksesi"

    In year 1, the phrase becomes "sch kn z zschrks cknnchc cyck zksceci" due to rules concerning the letter "x". In year 2, all "y"s are changed to "i"s yielding "sch kn z zschrks cknnchc cick zksceci". In year 3, "ce" and "ci" are changed to "se" and "si" yielding "sch kn z zschrks cknnchc sick zkssesi". In year 4, "ck" is changed to "k" and the phrase becomes "sch kn z zschrks knnchc sik zkssesi". In year 5, words that begin with "sch" are made to begin with "sk", "chr" is changed to "kr", and all "c"s not followed by an "h" are changed to "k" yielding "sk kn z zskrks knnchk sik zkssesi". In year 6, words that start with "kn" now start with "n" and the phrase becomes "sk n z zskrks nnchk sik zkssesi". Finally, in year 7, double consonants are removed yielding the final result.

  5. 7

    " concoction convalescence cyclical cello "

    Returns: " konkoktion konvalesense siklikal selo "

    Beware of extra spaces.

  6. 7

    ""

    Returns: ""

    Don't forget the empty case.

  7. 7

    "a sly gypsy yelled loudly by the gym yesterday"

    Returns: "a sli gipsi ieled loudli bi the gim iesterdai"

  8. 0

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sch kn x xschrx cknnchc cyck xxceci"

  9. 1

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sch kn z zschrks cknnchc cyck zksceci"

  10. 2

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sch kn z zschrks cknnchc cick zksceci"

  11. 3

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sch kn z zschrks cknnchc sick zkssesi"

  12. 4

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sch kn z zschrks knnchc sik zkssesi"

  13. 5

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sk kn z zskrks knnchk sik zkssesi"

  14. 6

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sk n z zskrks nnchk sik zkssesi"

  15. 6

    "magic circus giraffes suffer dazzling performances"

    Returns: "magik sirkus giraffes suffer dazzling performanses"

  16. 7

    "x"

    Returns: "z"

  17. 7

    "kn"

    Returns: "n"

  18. 7

    "aabbccddeeffgghhiijjkkll"

    Returns: "aabkdeefghiijkl"

  19. 7

    "mmnnooppqqrrssttuuvvwwxxyyzz"

    Returns: "mnoopqrstuuvwksksiiz"

  20. 7

    "the bookkeeper recalled rearranging the bookcase"

    Returns: "the bookeeper rekaled rearanging the bookase"

  21. 6

    "carcass circus caucus catchy catch chance charisma"

    Returns: "karkass sirkus kaukus katchi katch chanse charisma"

  22. 5

    "chancellor chocolate chaotic chronological chuckle"

    Returns: "chansellor chokolate chaotik kronologikal chukle"

  23. 4

    "church circa circle circuit circumstance chronicle"

    Returns: "church sirca sircle sircuit sircumstanse chronicle"

  24. 3

    "circumcise circumference cockroach clench chicken"

    Returns: "sircumsise sircumferense cockroach clench chicken"

  25. 7

    "chaos choice character charade chasm chemical"

    Returns: "chaos choise charakter charade chasm chemikal"

  26. 1

    "xylophone xenon xanthum exclaim expert sioux"

    Returns: "zylophone zenon zanthum eksclaim ekspert siouks"

  27. 7

    "scholar school schooner scholastic schwab"

    Returns: "skolar skool skooner skolastik skwab"

  28. 7

    "sch"

    Returns: "sk"

  29. 7

    " chandelier tom cellocck xzz zzx cck "

    Returns: " chandelier tom selok z zks k "

  30. 7

    "could my mighty knight always protect castles"

    Returns: "kould mi mighti night alwais protekt kastles"

  31. 7

    "cck xzz aaaaa"

    Returns: "k z aaaaa"

  32. 5

    "cchh"

    Returns: "kchh"

  33. 7

    "abba"

    Returns: "aba"

  34. 7

    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    Returns: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

  35. 7

    " concoction convalescence cyclical cello "

    Returns: " konkoktion konvalesense siklikal selo "

  36. 7

    "cck xzz aaaaa"

    Returns: "k z aaaaa"

  37. 7

    "vv"

    Returns: "v"

  38. 1

    "x"

    Returns: "z"

  39. 7

    "a b c d e"

    Returns: "a b k d e"

  40. 7

    "sch kn x xschrx cknnchc cyck xxceci"

    Returns: "sk n z zskrks nchk sik zksesi"

  41. 1

    "xoxox"

    Returns: "zoksoks"

  42. 6

    "kooc cooc cooc cooc cooc"

    Returns: "kook kook kook kook kook"

  43. 2

    "i fixed the chrome xerox by the cyclical church"

    Returns: "i fiksed the chrome zeroks bi the ciclical church"

  44. 5

    " concatenation cck"

    Returns: " konkatenation k"

  45. 7

    "sch chr zzbzzb aa bccb xx cei cb ci chr chk"

    Returns: "sk kr zbzb aa bkb zks sei kb si kr chk"

  46. 6

    "cooc cooc cooc cooc"

    Returns: "kook kook kook kook"

  47. 5

    "c"

    Returns: "k"

  48. 1

    "xenon xenon"

    Returns: "zenon zenon"

  49. 7

    " xx xx xxdxx knndsch sch schrrhc yiiyy knnsc "

    Returns: " zks zks zksdksks ndsch sk skrhk iiiii nsk "

  50. 7

    "thsx xstt cic tess ofc knkn zzoopp xox cyci"

    Returns: "thsks zst sik tes ofk nkn zoop zoks sisi"

  51. 7

    "aaa iii eeknee"

    Returns: "aaa iii eeknee"

  52. 1

    "xfdkjf xdksax"

    Returns: "zfdkjf zdksaks"

  53. 7

    "ch"

    Returns: "ch"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: