A compact range notation writes a run of characters as start-end. Expand such a notation into the full string it describes.

Walk the input from left to right. Whenever a character is followed by a dash and another character, that group is a range and expands to every character from the start through the end inclusive. A range whose start comes after its end expands backwards. Any character that is not part of a range is copied through unchanged.

If the input is null or empty, return an empty string.

Examples:

Input: spec = 'a-e' Output: 'abcde' Explanation: The single range runs forward from a to e Input: spec = 'a-cx-z' Output: 'abcxyz' Explanation: Two ranges sit side by side and each expands in turn Input: spec = 'e-a' Output: 'edcba' Explanation: The start comes after the end so the range counts backwards Input: spec = 'ab-d' Output: 'abcd' Explanation: The leading a is a plain character and b-d is a range Input: spec = '' Output: '' Explanation: An empty input produces an empty string
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay November 2026 (Nov 16–30)Sponsored by FLXBL

Contest runs November 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In