BadVocabulary
Member SRM 489 · 2010-03-12 · by fushar
Problem Statement
Little Teddy and Little Tracy are now learning how to speak words. Their mother, of course, doesn't want them to speak bad words. According to her definition, a word W is bad if at least one of the following conditions hold (see the notes section for definitions):
- W contains the string badPrefix as a prefix.
- W contains the string badSuffix as a suffix.
- W contains the string badSubstring as a contiguous substring that is neither a prefix nor a suffix of W.
You are given a
Notes
- A prefix of a string is obtained by removing zero or more contiguous characters from the end of the string.
- A suffix of a string is obtained by removing zero or more contiguous characters from the beginning of the string.
Constraints
- badPrefix, badSuffix, and badSubstring will each contain between 1 and 50 characters, inclusive.
- vocabulary will contain between 1 and 50 elements, inclusive.
- Each element vocabulary will contain between 1 and 50 characters, inclusive.
- Each character of badPrefix, badSuffix, and badSubstring will be between 'a' and 'z', inclusive.
- Each character in vocabulary will be between 'a' and 'z', inclusive.
- All elements of vocabulary will be distinct.
"bug"
"bug"
"bug"
{"buggy", "debugger", "debug"}
Returns: 3
"a"
"b"
"c"
{"a", "b", "tco"}
Returns: 3
"a" is a prefix of "a". "b" is a suffix of "b". "c" is a substring of "tco". So, all words in vocabulary are bad.
"cut"
"sore"
"scar"
{"scary", "oscar"}
Returns: 0
Although "scar" is a substring of "scary", it is also a prefix. Thus, "scary" is not a bad word. Similarly, as "scar" is a suffix of "oscar", "oscar" is not a bad word.
"bar"
"else"
"foo"
{"foofoofoo", "foobar", "elsewhere"}
Returns: 1
Only the word "foofoofoo" is bad.
"pre"
"s"
"all"
{"all", "coders", "be", "prepared", "for", "the", "challenge", "phase"}
Returns: 3
Submissions are judged against all 126 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BadVocabulary with a public method int count(string badPrefix, string badSuffix, string badSubstring, vector<string> vocabulary) · 126 test cases · 2 s / 256 MB per case