Acronym
SRM 83 · 2002-04-27 · by wyzmo
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.
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').
"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: ""
Submissions are judged against all 59 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Acronym with a public method string create(string phrase, string ignore) · 59 test cases · 2 s / 256 MB per case