String Compression
In String Compression, you are given an array of characters chars. Your goal is to compress it using the following algorithm:
Begin with an empty string s. For each group of consecutive repeating characters in chars:
- If the groupu0027s length is 1, append the character to
s. - Otherwise, append the character followed by the groupu0027s length.
The compressed string s should not be returned directly, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.
After you are done modifying the input array, return the new length of the array.
For example, chars = ["a","a","b","b","c","c","c"] becomes ["a","2","b","2","c","3"] and returns 6. If chars = ["a"], it returns 1 and the array stays ["a"].
Example Input & Output
Return length 6, array becomes ["a","2","b","2","c","3"].
Algorithm Flow

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:
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
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);
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
