Problem Statement
An acronym is a word formed by combining the initial letters of the words in a phrase. For example, an acronym for the phrase, "be right back", is "BRB". Often small words are ignored when forming acronyms from a phrase. For example, "USA" is an acronym for "United States of America" (the word "of" is ignored). Finally, when identical letters appear consecutively in an acronym, the repeating letters may be replaced by a single letter followed by the number of times the letter occurs. For example, "W3" is an acronym for "world wide web".
Write a method that, given a phrase and a set of words to ignore, returns the acronym for the phrase using the following rules:
- Select the initial letter of each word in the phrase except for those words that appear in the list of words to ignore.
- Convert each character selected for inclusion in the acronym to uppercase.
- Replace consecutive occurrences of the same letter within the acronym with a single occurrence of the letter followed by the number of times the letter occurs. For example, the acronym "AAABCCDEEEE" changes to "A3BC2DE4".
- If the resulting acronym is less than two characters in length (counting both letters and digits), return the empty string instead.
Definition
- Class:
- Acronym
- Method:
- create
- Parameters:
- String, String
- Returns:
- String
- Method signature:
- String create(String phrase, String ignore)
- (be sure your method is public)
Notes
- The words in both phrase and ignore are separated by one or more space characters. Either phrase or ignore may be the empty string, consist only of space characters, or have leading and trailing spaces.
Constraints
- both phrase and ignore are between 0 and 50 characters in length, inclusive.
- both phrase and ignore consist only of the space character (' ') and lowercase letters ('a'-'z').
Examples
"what you see is what you get"
"a an and the"
Returns: "WYSIWYG"
"x a xx a b x b b c c c c c c x c c c c c c c c c x"
"x"
Returns: "AXAB3C15"
"thoroughly tested through and through"
"the and an a"
Returns: "T4"
""
""
Returns: ""
"topcoder"
""
Returns: ""
"top coder"
""
Returns: "TC"
"the end"
""
Returns: "TE"
"quod erat demonstratum"
""
Returns: "QED"
"the end"
"a an and the"
Returns: ""
"the one and only"
"a and the"
Returns: "O2"
"the one and only"
"a and one the"
Returns: ""
"just in time"
""
Returns: "JIT"
"be right back"
""
Returns: "BRB"
"every good boy does fine"
""
Returns: "EGBDF"
"world wide web consortium"
""
Returns: "W3C"
"hydrogen hydrogen oxygen"
""
Returns: "H2O"
"rolling on the floor laughing"
"a an and the"
Returns: "ROFL"
"the quick brown fox jumps over the lazy dog"
"a an the"
Returns: "QBFJOLD"
"an easy and medium coding contest"
"a an and the"
Returns: "EMC2"
"world wide web"
""
Returns: "W3"
"the the"
"dog cat mouse"
Returns: "T2"
"word"
""
Returns: ""
"wsdf wfgg wfgr"
""
Returns: "W3"
"with this you die"
"you"
Returns: "WTD"
"me and"
"and"
Returns: ""
"i hope hope hope this will will fifty fifty ps"
"p ps me get"
Returns: "IH3TW2F2"
"this"
""
Returns: ""
"world wide"
""
Returns: "W2"
"abc abc abc abc efg efg"
"dabc"
Returns: "A4E2"
"w w w w w w w w w w w we we de ee ee ee ee e "
"pp p p p ep e e e d ewf ewf ewf fewr fewr"
Returns: "W13DE4"
"hello there"
"there"
Returns: ""
"world wide web big black box aaaaa aaaaaaa aaaaa"
""
Returns: "W3B3A3"
" a kek fkjai asdijidsfj kdjfkd "
""
Returns: "AKFAK"
"wrt wlif wker"
""
Returns: "W3"
"no good"
""
Returns: "NG"
"a b c d a a a b c c c d d"
"a c"
Returns: "BDBD2"
"a b c d a a a b c c c d d"
"cap c"
Returns: "ABDA3BD2"
"ab ba lock"
"bah treh"
Returns: "ABL"
"alans aim"
""
Returns: "A2"
"about big and top ten "
"sand"
Returns: "ABAT2"
"tp"
""
Returns: ""
"to to to to to to to to to to "
""
Returns: "T10"
"the little red riding hood to hood"
"hood the"
Returns: "LR2T"
"top coder apple cow"
"atopco"
Returns: "TCAC"
"it will not work with wide the wacky w words or os"
"the"
Returns: "IWNW6O2"
"thourghly tested through and through"
"and the end stuff"
Returns: "T4"
"topcoder"
"top"
Returns: ""
"an an an an an an an an an an an an an an an an an"
""
Returns: "A17"
"we we we we we we we we we w wew e wew e we we w"
""
Returns: "W11EWEW3"
"about big and top ten"
"sand atop aten "
Returns: "ABAT2"
"the little red rigdd hood hood s"
"the hood"
Returns: "LR2S"
"ha ah ha ah zoo ooz ok oks boo"
"z ha"
Returns: "A2ZO3B"
"what why when you see iz what you will get"
"will"
Returns: "W3YSIWYG"
"fred was here yes he was"
"fred here he"
Returns: "WYW"
"a a a a a a a a a a a a a a b b b b b b b b b b"
""
Returns: "A14B10"
"world wide web wunsux animal pet"
""
Returns: "W4AP"
"topcoder"
"the an"
Returns: ""
"the little red right hood hooh the s"
"the hood"
Returns: "LR2HS"
"thoroughly tested through and through"
"and"
Returns: "T4"