TopcoderArchive
Archive
Algorithm problem

PalindromePatterns

Problem statement, definition, constraints, and public examples.

Problem Statement

Using only the letters 'a' and 'b' return how many palindromes of length len do not have forbidden as a substring. A palindrome is a word that is unchanged by reversing it.

Definition

Class:
PalindromePatterns
Method:
howMany
Parameters:
String, int
Returns:
long
Method signature:
long howMany(String forbidden, int len)
(be sure your method is public)

Constraints

  • forbidden will contain between 1 and 5 characters inclusive.
  • len will be between 1 and 100 inclusive.
  • Each character in forbidden must be 'a' or 'b'.

Examples

  1. "a"
    5
    Returns: 1
    Only one possible palindrome.
  2. "aaaaa"
    5
    Returns: 7
    7 of the 8 possible palindromes work here.
  3. "aa"
    10
    Returns: 8
  4. "aa"
    30
    Returns: 987
  5. "a"
    1
    Returns: 1
Back to all problems