Strings Problem Set 10

Part I - Raptor Programs

String.28 Prefix Again

Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().

  1. prefixAgain("abXYabc", 1) . true
  2. prefixAgain("abXYabc", 2) . true
  3. prefixAgain("abXYabc", 3) . false

Solutions:

python

raptor

String.29 XYZ Middle

Given a string, does "xyz" appear in the middle of the string? To define middle, we'll say that the number of chars to the left and right of the "xyz" must differ by at most one. This problem is harder than it looks.

  1. xyzMiddle("AAxyzBB") . true
  2. xyzMiddle("AxyzBB") . true
  3. xyzMiddle("AxyzBBB") . false

Solutions:

python

raptor

String.30 Get Sandwich

A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.

  1. getSandwich("breadjambread") . "jam"
  2. getSandwich("xxbreadjambreadyy") . "jam"

Solutions:

python

raptor