TopcoderArchive
Archive22 Aug 2006
TCCC06 Round 1A

StringSquares

Problem statement, definition, constraints, and public examples.

Problem Statement

We call a string a square if it is a concatenation of two equal non-empty strings. All string comparisons in this problem are case sensitive. For example, "aa", "ABAB", and "abcabc" are squares, but "a", "aaa", "ABCabc", and "abcab" are not.

Given a String s, return the number of distinct squares that appear as contiguous substrings of s. For example, in "aaabccabccCC", you can find the squares "aa", "abccabcc", "cc", and "CC", so you would return 4. The squares "aa" and "cc" each appear twice in the string, but they're only counted once because we're only interested in distinct squares.

Definition

Class:
StringSquares
Method:
count
Parameters:
String
Returns:
int
Method signature:
int count(String s)
(be sure your method is public)

Constraints

  • s will contain between 0 and 50 characters, inclusive.
  • s will contain only letters ('a'-'z', 'A'-'Z').

Examples

  1. "aaabccabccCC"
    Returns: 4
    The example in the problem statement.
  2. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    Returns: 21
  3. "cc"
    Returns: 1
  4. "B"
    Returns: 0
  5. "ABCDABCDabcdabcdABCDABCDabcdabcd"
    Returns: 3
Back to all problems