TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

StringFind

Problem statement, definition, constraints, and public examples.

Problem Statement

You will be a given a String text and a String part. You will return an int representing how many times part occurs in text as a substring. For example:

text = "aaaabbbb"
  • If part = "ab" then you would return 1 since ab occurs only once in text.
  • If part = "aa" then you would return 3 since there are 3 distinct occurrences of aa in text.
  • If part = "aaa" then you would return 2 since there are 2 distinct occurrences of aaa in text.
  • If part = "ba" then you would return 0 since ba does not occur in text.

Definition

Class:
StringFind
Method:
howMany
Parameters:
String, String
Returns:
int
Method signature:
int howMany(String text, String part)
(be sure your method is public)

Constraints

  • text will contain between 1 and 50 characters inclusive.
  • Each character in text will be a lowercase letter ('a'-'z').
  • part will contain between 1 and 50 characters inclusive.
  • Each character in part will be a lowercase letter ('a'-'z').

Examples

  1. "aaaabbbb"
    "ab"
    Returns: 1
    From above
  2. "aaaabbbb"
    "aa"
    Returns: 3
    From above
  3. "ladidadi"
    "z"
    Returns: 0
    No z's here
  4. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aa"
    Returns: 49
    Lots of a's
Back to all problems