BudiBadu Logo

Lantern Pattern Paths

Array Easy 0 views
Like4

Designers at the Festival of Threads create riverside mazes using lantern arcs. Each design starts with a `base` walkway phrase. Curators then wrap it in multiple mirrored corridors. Your task in Lantern Pattern Paths is to generate the full usher script for this layered experience.

The "secret sauce" here is Recursive Wrapping. Each corridor (named marker) wraps around the current script with three new lines and a second pass through the inner path. For each marker, ushers recite: "Enter {marker}", the current full script, "Mirror {marker}", the script again, and finally "Exit {marker}". If markers is empty, return just the base phrase. This woven experience creates a hypnotic rhythm using a symmetrical pattern! It’s a great way to practice modeling growth through recursive string manipulation. Return an array of strings describing every phrase in the precise order guests will hear them.

When the task involves connectivity or route cost, build adjacency carefully and guard against revisiting stale states. Use a visited or best-distance structure to avoid repeated work, and ensure unreachable scenarios return the required fallback value instead of partial traversal results.

Examples

Example 1
Input
base = "Pause by the willow.", markers = []
Output
["Pause by the willow."]
Explanation

Example with input: base = "Pause by the willow.", markers = []

Example 2
Input
base = "Listen for distant bells.", markers = ["Golden Spine", "Silver Veil"]
Output
["Enter Golden Spine", "Enter Silver Veil", "Listen for distant bells.", "Mirror Silver Veil", "Listen for distant bells.", "Exit Silver Veil", "Mirror Golden Spine", "Enter Silver Veil", "Listen for distant bells.", "Mirror Silver Veil", "Listen for distant bells.", "Exit Silver Veil", "Exit Golden Spine"]
Explanation

Example with input: base = "Listen for distant bells.", markers = ["Go

Example 3
Input
base = "Pause by the willow.", markers = ["Azure Bridge"]
Output
["Enter Azure Bridge", "Pause by the willow.", "Mirror Azure Bridge", "Pause by the willow.", "Exit Azure Bridge"]
Explanation

Example with input: base = "Pause by the willow.", markers = ["Azure B

Algorithm Flow

Recommendation Algorithm Flow for Lantern Pattern Paths - Budibadu
Recommendation Algorithm Flow for Lantern Pattern Paths - Budibadu

Best Answers

java
class Solution {
    public int lantern_pattern_paths(int m, int n) {
        return (int) nCr(m + n - 2, m - 1);
    }
    private long nCr(int n, int r) {
        if (r > n) return 0;
        if (r == 0 || r == n) return 1;
        if (r > n / 2) r = n - r;
        long res = 1;
        for (int i = 1; i <= r; i++) {
            res = res * (n - i + 1) / i;
        }
        return res;
    }
}