Canyon Echo Verse
This challenge becomes much easier once you know exactly what to keep, change, or count. In Canyon Echo Verse, you are trying to work toward the right number by following one clear idea.
Generate recursive echo pattern for storytelling A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.
For example, if the input is layers = 0, the answer is 1. Only the narrator speaks, so one verse is heard. Another example is layers = 4, which gives 49. Four layers continue the same pattern, leading to forty-nine verses echoing through the canyon.
This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.
One helpful habit is to say the rule out loud in your own words before you start solving. If you can explain what counts, what changes, and what the final answer should look like, you are already much closer to the right solution.
Example Input & Output
Only the narrator speaks, so one verse is heard.
The second layer adds a verse and repeats the earlier chorus twice.
Four layers continue the same pattern, leading to forty-nine verses echoing through the canyon.
Algorithm Flow
Solution Approach
This problem asks us to repeat a verse a given number of times and join the copies with spaces. Each "echo" adds one more copy of the original verse, so the total number of copies is echoes + 1 (the original plus each echo).
The simplest approach is to create an array filled with the verse repeated echoes + 1 times, then join it with a single space between each copy.
Here is the implementation:
new Array(echoes + 1) creates an array of the right length, .fill(verse) puts the verse in every slot, and .join(' ') connects the copies with spaces. The result is the verse repeated exactly echoes + 1 times.
For example, with verse = "Hello" and echoes = 2, we get "Hello Hello Hello" — the original plus two echoes. When echoes = 0, we get just "Hello".
The time and space complexity are both O(echoes), since we build and join an array of echoes + 1 elements.
Best Answers
import java.util.*;
class Solution {
public String canyon_echo_verse(String verse, int echoes) {
String[] parts = new String[echoes + 1];
Arrays.fill(parts, verse);
return String.join(" ", parts);
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
