Amazon | Phone screen | First occurrence in a two-word string

Given a string of the form "BLAHBLAHBLAHBLAHBLAH...BLAHAPPLEAPPLEAPPLEAPPLE...APPLE", i.e., consisting of a bunch of concatenated "BLAH"s followed by a bunch of concatenated "APPLE"s, find the starting index of the first occurence of "APPLE". Both "BLAH" and "APPLE" may appear zero or more times.

Examples

  1. "BLAHBLAHBLAH" => return -1 because "APPLE" does not appear at all
  2. "APPLEAPPLEAPPLE" => return 0 becase "APPLE" appears right at the beginning
  3. "BLAHBLAHBLAHAPPLEAPPLEAPPLE" => return 12 because that is the first place where "APPLE" occurs
Comments (7)