# Phrase FRQ Solution

The third question on the 2017 AP Computer Science exam asked you to process a string, replacing the $Nth$ occurrence and finding the last occurrence.

As part of the question you were given a non-implemented method called findNthOccurrence that you were expected to use. It looked like this on the test.

public int findNthOccurrence(String str, int n) {
// Implementation not shown
}


If they’re going to give you a method stub like this, you should expect to use it somewhere in your solution. They probably wouldn’t bother defining a method like this if you weren’t expected to use it.

## Part A

For part A you were to replace the $Nth$ occurrence of a substring in currentPhrase. They gave you a series of examples like this.

Phrase phrase1 = new Phrase("A cat ate late.");
phrase1.replaceNthOccurrence("at", 1, "rane");
System.out.println(phrase1);


The snippet should output "A crane at late." assuming you implement replaceNthOccurrence correctly.

Here is a working solution.

public void replaceNthOccurrence(String str, int n, String repl) {
int idxStart = findNthOccurrence(str, n);
if (idxStart >= 0) {
String front = currentPhrase.substring(0, idxStart);
String back = currentPhrase.substring(idxStart + str.length());
currentPhrase = front + repl + back;
}
}


We find the $Nth$ occurrence using the provided findNthOccurrence method. Note that we don’t see how findNthOccurrence is implemented, and we don’t really have to care.

If idxStart $\ge 0$ then the substring was found and we’ve got some work to do. If idxStart $\lt 0$ then it wasn’t found and we’re not going to change currentPhrase.

I made a variable called front that contains everything up to the substring we’re looking for and a variable called back of everything after it. The code the returns front concatenated with repl followed by back which creates a string with the $Nth$ occurrence of str replaced with repl.

## Part B

The second part tasked you with finding the index of the last occurrence of a substring. And it specifically tells you that you must use findNthOccurrence to get full credit.

public int findLastOccurrence(String str) {
int lastIdx = -1;
int n = 0;
while (findNthOccurrence(str, n) > -1) {
lastIdx = findNthOccurrence(str, n);
n++;
}
return lastIdx;
}


This code keeps incrementing n as it looks for the $Nth$ occurrence. When that index value is -1 it means that the previous index was the last index.

This question bothered me a bit though. It seemed very arbitrary to have to solve this problem in this way when there’s a one line solution that’s using a method related to one in the AP subset. You should have learned about the indexOf method on a String that tells you where a substring is found. You may have also learned about lastIndexOf which tells you where the last occurrence of a substring is. lastIndexOf is not formally part of the AP Java subset, but many students learn about it because it’s so close to indexOf.

public int findLastOccurrence(String str) {
return currentPhrase.lastIndexOf(str);
}


I would imagine there will be a lot of packets with a similar solution written. Functionally using lastIndexOf would work, but wouldn’t get full credit since it doesn’t meet the requirement to use findNthOccurrence.

This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.