Connection Status:
Competition Arena > SyllableCountEstimator
SRM 798 · 2021-01-22 · by misof · Simple Search, Iteration, String Manipulation
Class Name: SyllableCountEstimator
Return Type: int
Method Name: estimate
Arg Types: (string)
Problem Statement

Problem Statement

You are given a String W. Your task is to implement a simple set of rules that will help us estimate how many syllables W has when pronounced as an English word.

Each letter is either a vowel or a consonant. In this problem the vowels are 'a', 'e', 'i', 'o', and 'u'. Note that in this problem 'y' is always a consonant. The rules for counting syllables are as follows:

  1. In general, the number of syllables is equal to the number of vowels in the word.
  2. There are groups of vowels that usually produce fewer syllables if they occur together. For the purpose of our estimate, these groups are "au", "oa", "oo", and "iou". To make it simple, we subtract 1 from the syllable count for each occurrence of each of these vowel groups. (All occurrences count, even if they are a part of a bigger group of vowels.)
  3. Subtract 1 if the word ends in 'e'.
  4. Add 1 if the word ends in "le" and the previous letter exists and is a consonant.
  5. If the result is less than 1, set it to 1.

Return the number of syllables in the given W estimated using the above sequence of rules.

Constraints

  • W will contain between 1 and 20 characters, inclusive.
  • Each character in W will be a lowercase English letter ('a'-'z').
Examples
0)
"potato"
Returns: 3

The string "potato" has three vowels ('o', 'a', 'o'). No other rules apply, so our estimate says it should have three syllables.

1)
"haul"
Returns: 1

Two vowels, but we subtract 1 for the group "au".

2)
"gooooooal"
Returns: 1

The groups of vowels may overlap. For example, this W contains five occurrences of "oo" and one occurrence of "oy". Thus, the answer is (7 vowels) minus (5 occurrences of "oo") minus (1 occurrence of "oa") = 1.

3)
"rhythm"
Returns: 1

Our rules are not perfect. In actual English the word "rhythm" has two syllables, but our rules produce an incorrect estimate: According to rule #1, there are no 'a', 'e', 'i', 'o', 'u' in this W so the current number of syllables is 0. Rules #2, #3, #4 do not apply. In rule #5 we fix the number of syllables from 0 to 1, which is our final answer.

4)
"e"
Returns: 1

The given W does not have to be a valid English word. When estimating the number of syllables for "e", we set it to 1 in rule #1, then decrease it to 0 in rule #3, and then increase it back to 1 in rule #5.

6)
"various"
Returns: 3

This W contains an occurrence of "iou".

7)
"queued"
Returns: 4

Another case when our simple estimate doesn't match reality.

Submissions are judged against all 84 archived test cases, of which 7 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SyllableCountEstimator with a public method int estimate(string W) · 84 test cases · 2 s / 256 MB per case

Submitting as anonymous