TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

ExceptAfterC

Problem statement, definition, constraints, and public examples.

Problem Statement

English spelling is full of crazy rules. One rule that children learn is "I before E except after C", meaning that whenever an 'I' and an 'E' are adjacent, the 'I' must precede the 'E' unless the pair is immediately after a 'C', in which case the 'E' must precede the 'I'.

Write a method that checks compliance with this rule. The method takes a String word and returns the number of times the rule is violated in word. Notice that a single letter can be involved in more than one violation of the rule (see Example 4).

Definition

Class:
ExceptAfterC
Method:
violations
Parameters:
String
Returns:
int
Method signature:
int violations(String word)
(be sure your method is public)

Constraints

  • word contains between 1 and 25 characters, inclusive.
  • Every character in word is an uppercase letter ('A'-'Z', inclusive).

Examples

  1. "PIES"
    Returns: 0
  2. "RECIEVE"
    Returns: 1
  3. "RECEIVE"
    Returns: 0
  4. "NEIGHBOR"
    Returns: 1
    In this simplified version of "I before E except after C", ignore the usual extension "or when pronounced as A". So the "EI" in "NEIGHBOR" is an error for the purposes of this problem, even though it is okay in English.
  5. "ACIEIB"
    Returns: 2
    The 'E' is involved in two separate violations: first in the "IE" after the 'C', and then in the "EI".
  6. "EIEIEICIEIEIE"
    Returns: 6
Back to all problems