Code Logo

Ember Glyph Mosaic

Published at05 Jan 2026
2D Array Easy 19 views
Like27

This problem feels like a little puzzle you can solve one step at a time. In Ember Glyph Mosaic, you are trying to work toward the right list by following one clear idea.

Build glyph mosaic with expanding rings 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 base = ["*"], bands = ["North"], the answer is ["North^", "*", "vNorth", "*"]. Example with input: base = ["*"], bands = ["North"] Another example is base = ["*"], bands = [], which gives ["*"]. Example with input: base = ["*"], bands = []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

Example 1
Input
base = ["*"], bands = ["North"]
Output
["North^", "*", "vNorth", "*"]
Explanation

Example with input: base = ["*"], bands = ["North"]

Example 2
Input
base = ["*"], bands = []
Output
["*"]
Explanation

Example with input: base = ["*"], bands = []

Example 3
Input
base = [".o.", "ooo"], bands = ["Amber", "Quartz"]
Output
["Amber^", "Quartz^", ".o.", "ooo", "vQuartz", ".o.", "ooo", "Amber^", "Quartz^", ".o.", "ooo", "vQuartz", ".o.", "ooo", "vAmber", "Quartz^", ".o.", "ooo", "vQuartz", ".o.", "ooo"]
Explanation

Example with input: base = [".o.", "ooo"], bands = ["Amber", "Quartz"]

Algorithm Flow

Recommendation Algorithm Flow for Ember Glyph Mosaic

Solution Approach

This problem asks us to build a recursively expanding glyph mosaic. Starting from a base list, each band wraps the current result with a marker and also repeats it, producing a layered structure. The recursion mirrors the nesting: each band processes the bands that follow it, then adds its own layer around the result.

The key idea is that for each band, the final output is [marker^, ...inner, vmarker, ...inner] — the marker's opening, followed by the inner mosaic, followed by the marker's closing, followed by the inner mosaic a second time. The inner part is computed by recursing on the remaining bands.

Here is the implementation:

function ember_glyph_mosaic(base, bands) {
    function build(current, remainingBands) {
        if (remainingBands.length === 0) return [...current];
        const marker = remainingBands[0];
        const inner = build(current, remainingBands.slice(1));
        return [`${marker}^`, ...inner, `v${marker}`, ...inner];
    }
    return build(base, bands);
}

The base case is when there are no bands left, in which case we return a copy of the current list. Otherwise we take the first band as the marker, recursively build the inner mosaic from the remaining bands, and assemble the output by placing the opening marker, the inner mosaic, the closing marker, and the inner mosaic again.

Let us trace base = ["*"], bands = ["North"]. With one band, inner is just the base ["*"]. The result becomes ["North^", "*", "vNorth", "*"], matching the expected output. When bands is empty, the base case returns the base unchanged, so the answer is ["*"].

Notice that the inner mosaic appears twice, which is why the output grows so quickly as more bands are added. The total length is roughly 2^(number of bands) times the base length.

The time complexity is O(L) where L is the final output length, and the space complexity is also O(L) because of the repeated nested lists.

Best Answers

java
import java.util.*;

class Solution {
    public String[] ember_glyph_mosaic(String[] base, String[] bands) {
        return build(Arrays.asList(base), bands, 0).toArray(new String[0]);
    }
    
    private List<String> build(List<String> current, String[] bands, int idx) {
        if (idx >= bands.length) return new ArrayList<>(current);
        String marker = bands[idx];
        List<String> inner = build(current, bands, idx + 1);
        List<String> result = new ArrayList<>();
        result.add(marker + "^");
        result.addAll(inner);
        result.add("v" + marker);
        result.addAll(inner);
        return result;
    }
}