Code Logo

Market Slate Phrase

Published at05 Jan 2026
Easy 3 views
Like22

In Market Slate Phrase, you are given a list of title strings and need to turn them into one final line of text.

The job has two parts. First, sort the titles into the order the examples expect. Second, join the sorted titles with | between each one. You are not supposed to change the words themselves. Keep the original spelling, spacing, and casing of every title, and keep duplicates too.

For example, if the input is titles = ["ember","Aurora Pulse","Beacon"], the output should be Aurora Pulse | Beacon | ember. If the input is titles = ["A","a","B","b"], the output should be a | A | b | B. That second example is important because it shows the ordering is not just about the first visible character; the final result follows the exact comparison rule used by the judge.

If the array is empty, return an empty string. If it has one title, return that title by itself. So the task is to sort the strings correctly, then build one phrase using bar separators.

Example Input & Output

Example 1
Input
titles = ["ember","Aurora Pulse","Beacon"]
Output
Aurora Pulse | Beacon | ember
Explanation

The titles are sorted first, then joined into one line with bar separators.

Example 2
Input
titles = ["Zan","abc","DEF"]
Output
abc | DEF | Zan
Explanation

The final phrase keeps the original text of each title while placing them in the expected order.

Example 3
Input
titles = ["A","a","B","b"]
Output
a | A | b | B
Explanation

This example shows the exact tie behavior used when lowercase and uppercase versions of the same letter appear together.

Algorithm Flow

Recommendation Algorithm Flow for Market Slate Phrase
Recommendation Algorithm Flow for Market Slate Phrase

Solution Approach

A clean way to solve this problem is to make a sorted copy of the titles and then join them with the string " | ". That keeps the whole solution close to what the problem actually asks for: first order the strings, then produce one final formatted phrase.

The most important detail is the string comparison rule. The examples show that casing should be preserved in the output, but the ordering is not a simple raw ASCII sort. The case pair example ["A", "a", "B", "b"] -> "a | A | b | B" shows that lowercase and uppercase versions of the same letter do not collapse together, and the lowercase form comes first when the base letters match. So the safest approach is to use a comparator that follows natural alphabetical comparison while still preserving the original text. In JavaScript, that usually means localeCompare with settings that treat letters naturally and prefer lowercase first on ties.

The core idea looks like this:

const sorted = [...titles].sort((a, b) =>
  a.localeCompare(b, undefined, { sensitivity: 'base', caseFirst: 'lower' })
);
return sorted.join(' | ');

The spread operator creates a copy, which is useful if you do not want to mutate the original input. After sorting, join(' | ') builds the exact output format the problem wants. That also handles edge cases naturally. An empty array becomes an empty string. A one-element array becomes that same one title with no extra separators. Duplicates stay in the result because sorting does not remove them.

This approach is also easy to reason about. All formatting happens after ordering is finished, so you do not mix the comparison logic with the output-building logic. That separation makes the code cleaner and easier to debug if an example comes out in the wrong order.

The time complexity is O(n log n) because sorting dominates the work. Joining the titles takes linear time after that. Extra space is O(n) if you create a copy before sorting, plus the output string itself. So the full strategy is: sort the titles using the same comparison behavior shown by the examples, then join them with | and return the finished phrase.

Best Answers

java
import java.util.*;

class Solution {
    public String market_slate_phrase(String[] titles) {
        if (titles.length == 0) return "";
        String[] sorted = titles.clone();
        Arrays.sort(sorted);
        return String.join(" | ", sorted);
    }
}